Reland "Trim down FileWrapper class to be merely a wrapper owning a FILE*"

This is a reland of 80b95de7651caa0cfeb684ffc200860989f667dc

Original change's description:
> Trim down FileWrapper class to be merely a wrapper owning a FILE*
> 
> Bug: webrtc:6463
> Change-Id: If71e2f3a75dc1863bc805ab71de1e2d33294f805
> Reviewed-on: https://webrtc-review.googlesource.com/c/117881
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Reviewed-by: Alex Loiko <aleloi@webrtc.org>
> Commit-Queue: Niels Moller <nisse@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#26311}

Bug: webrtc:6463
Change-Id: I12154ef65744c1b7811974a1d871e05ed3fbbc27
Reviewed-on: https://webrtc-review.googlesource.com/c/118660
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26337}
This commit is contained in:
Niels Möller
2019-01-21 11:59:10 +01:00
committed by Commit Bot
parent 700615fb1c
commit 5a6ae02e90
13 changed files with 229 additions and 345 deletions

View File

@ -67,14 +67,13 @@ class DebugDumpWriterImpl final : public DebugDumpWriter {
#endif #endif
private: private:
std::unique_ptr<FileWrapper> dump_file_; FileWrapper dump_file_;
}; };
DebugDumpWriterImpl::DebugDumpWriterImpl(FILE* file_handle) DebugDumpWriterImpl::DebugDumpWriterImpl(FILE* file_handle) {
: dump_file_(FileWrapper::Create()) {
#if WEBRTC_ENABLE_PROTOBUF #if WEBRTC_ENABLE_PROTOBUF
dump_file_->OpenFromFileHandle(file_handle); dump_file_ = FileWrapper(file_handle);
RTC_CHECK(dump_file_->is_open()); RTC_CHECK(dump_file_.is_open());
#else #else
RTC_NOTREACHED(); RTC_NOTREACHED();
#endif #endif
@ -110,7 +109,7 @@ void DebugDumpWriterImpl::DumpNetworkMetrics(
*metrics.uplink_recoverable_packet_loss_fraction); *metrics.uplink_recoverable_packet_loss_fraction);
} }
DumpEventToFile(event, dump_file_.get()); DumpEventToFile(event, &dump_file_);
#endif // WEBRTC_ENABLE_PROTOBUF #endif // WEBRTC_ENABLE_PROTOBUF
} }
@ -143,7 +142,7 @@ void DebugDumpWriterImpl::DumpEncoderRuntimeConfig(
if (config.num_channels) if (config.num_channels)
dump_config->set_num_channels(*config.num_channels); dump_config->set_num_channels(*config.num_channels);
DumpEventToFile(event, dump_file_.get()); DumpEventToFile(event, &dump_file_);
#endif // WEBRTC_ENABLE_PROTOBUF #endif // WEBRTC_ENABLE_PROTOBUF
} }
@ -157,7 +156,7 @@ void DebugDumpWriterImpl::DumpControllerManagerConfig(
event.set_type(Event::CONTROLLER_MANAGER_CONFIG); event.set_type(Event::CONTROLLER_MANAGER_CONFIG);
event.mutable_controller_manager_config()->CopyFrom( event.mutable_controller_manager_config()->CopyFrom(
controller_manager_config); controller_manager_config);
DumpEventToFile(event, dump_file_.get()); DumpEventToFile(event, &dump_file_);
} }
#endif // WEBRTC_ENABLE_PROTOBUF #endif // WEBRTC_ENABLE_PROTOBUF

View File

@ -43,15 +43,10 @@ FileAudioDevice::FileAudioDevice(const char* inputFilename,
_recording(false), _recording(false),
_lastCallPlayoutMillis(0), _lastCallPlayoutMillis(0),
_lastCallRecordMillis(0), _lastCallRecordMillis(0),
_outputFile(*FileWrapper::Create()),
_inputFile(*FileWrapper::Create()),
_outputFilename(outputFilename), _outputFilename(outputFilename),
_inputFilename(inputFilename) {} _inputFilename(inputFilename) {}
FileAudioDevice::~FileAudioDevice() { FileAudioDevice::~FileAudioDevice() {}
delete &_outputFile;
delete &_inputFile;
}
int32_t FileAudioDevice::ActiveAudioLayer( int32_t FileAudioDevice::ActiveAudioLayer(
AudioDeviceModule::AudioLayer& audioLayer) const { AudioDeviceModule::AudioLayer& audioLayer) const {
@ -210,13 +205,15 @@ int32_t FileAudioDevice::StartPlayout() {
} }
// PLAYOUT // PLAYOUT
if (!_outputFilename.empty() && if (!_outputFilename.empty()) {
!_outputFile.OpenFile(_outputFilename.c_str(), false)) { _outputFile = FileWrapper::OpenWriteOnly(_outputFilename.c_str());
RTC_LOG(LS_ERROR) << "Failed to open playout file: " << _outputFilename; if (!_outputFile.is_open()) {
_playing = false; RTC_LOG(LS_ERROR) << "Failed to open playout file: " << _outputFilename;
delete[] _playoutBuffer; _playing = false;
_playoutBuffer = NULL; delete[] _playoutBuffer;
return -1; _playoutBuffer = NULL;
return -1;
}
} }
_ptrThreadPlay.reset(new rtc::PlatformThread( _ptrThreadPlay.reset(new rtc::PlatformThread(
@ -246,7 +243,7 @@ int32_t FileAudioDevice::StopPlayout() {
_playoutFramesLeft = 0; _playoutFramesLeft = 0;
delete[] _playoutBuffer; delete[] _playoutBuffer;
_playoutBuffer = NULL; _playoutBuffer = NULL;
_outputFile.CloseFile(); _outputFile.Close();
RTC_LOG(LS_INFO) << "Stopped playout capture to output file: " RTC_LOG(LS_INFO) << "Stopped playout capture to output file: "
<< _outputFilename; << _outputFilename;
@ -267,13 +264,16 @@ int32_t FileAudioDevice::StartRecording() {
_recordingBuffer = new int8_t[_recordingBufferSizeIn10MS]; _recordingBuffer = new int8_t[_recordingBufferSizeIn10MS];
} }
if (!_inputFilename.empty() && if (!_inputFilename.empty()) {
!_inputFile.OpenFile(_inputFilename.c_str(), true)) { _inputFile = FileWrapper::OpenReadOnly(_inputFilename.c_str());
RTC_LOG(LS_ERROR) << "Failed to open audio input file: " << _inputFilename; if (!_inputFile.is_open()) {
_recording = false; RTC_LOG(LS_ERROR) << "Failed to open audio input file: "
delete[] _recordingBuffer; << _inputFilename;
_recordingBuffer = NULL; _recording = false;
return -1; delete[] _recordingBuffer;
_recordingBuffer = NULL;
return -1;
}
} }
_ptrThreadRec.reset(new rtc::PlatformThread( _ptrThreadRec.reset(new rtc::PlatformThread(
@ -304,7 +304,7 @@ int32_t FileAudioDevice::StopRecording() {
delete[] _recordingBuffer; delete[] _recordingBuffer;
_recordingBuffer = NULL; _recordingBuffer = NULL;
} }
_inputFile.CloseFile(); _inputFile.Close();
RTC_LOG(LS_INFO) << "Stopped recording from input file: " << _inputFilename; RTC_LOG(LS_INFO) << "Stopped recording from input file: " << _inputFilename;
return 0; return 0;

View File

@ -154,8 +154,8 @@ class FileAudioDevice : public AudioDeviceGeneric {
int64_t _lastCallPlayoutMillis; int64_t _lastCallPlayoutMillis;
int64_t _lastCallRecordMillis; int64_t _lastCallRecordMillis;
FileWrapper& _outputFile; FileWrapper _outputFile;
FileWrapper& _inputFile; FileWrapper _inputFile;
std::string _outputFilename; std::string _outputFilename;
std::string _inputFilename; std::string _inputFilename;
}; };

View File

@ -55,7 +55,7 @@ void CopyFromConfigToEvent(const webrtc::InternalAPMConfig& config,
} // namespace } // namespace
AecDumpImpl::AecDumpImpl(std::unique_ptr<FileWrapper> debug_file, AecDumpImpl::AecDumpImpl(FileWrapper debug_file,
int64_t max_log_size_bytes, int64_t max_log_size_bytes,
rtc::TaskQueue* worker_queue) rtc::TaskQueue* worker_queue)
: debug_file_(std::move(debug_file)), : debug_file_(std::move(debug_file)),
@ -196,7 +196,7 @@ void AecDumpImpl::WriteRuntimeSetting(
} }
std::unique_ptr<WriteToFileTask> AecDumpImpl::CreateWriteToFileTask() { std::unique_ptr<WriteToFileTask> AecDumpImpl::CreateWriteToFileTask() {
return absl::make_unique<WriteToFileTask>(debug_file_.get(), return absl::make_unique<WriteToFileTask>(&debug_file_,
&num_bytes_left_for_log_); &num_bytes_left_for_log_);
} }
@ -204,24 +204,20 @@ std::unique_ptr<AecDump> AecDumpFactory::Create(rtc::PlatformFile file,
int64_t max_log_size_bytes, int64_t max_log_size_bytes,
rtc::TaskQueue* worker_queue) { rtc::TaskQueue* worker_queue) {
RTC_DCHECK(worker_queue); RTC_DCHECK(worker_queue);
std::unique_ptr<FileWrapper> debug_file(FileWrapper::Create());
FILE* handle = rtc::FdopenPlatformFileForWriting(file); FILE* handle = rtc::FdopenPlatformFileForWriting(file);
if (!handle) { if (!handle) {
return nullptr; return nullptr;
} }
if (!debug_file->OpenFromFileHandle(handle)) { return absl::make_unique<AecDumpImpl>(FileWrapper(handle), max_log_size_bytes,
return nullptr; worker_queue);
}
return absl::make_unique<AecDumpImpl>(std::move(debug_file),
max_log_size_bytes, worker_queue);
} }
std::unique_ptr<AecDump> AecDumpFactory::Create(std::string file_name, std::unique_ptr<AecDump> AecDumpFactory::Create(std::string file_name,
int64_t max_log_size_bytes, int64_t max_log_size_bytes,
rtc::TaskQueue* worker_queue) { rtc::TaskQueue* worker_queue) {
RTC_DCHECK(worker_queue); RTC_DCHECK(worker_queue);
std::unique_ptr<FileWrapper> debug_file(FileWrapper::Create()); FileWrapper debug_file = FileWrapper::OpenWriteOnly(file_name.c_str());
if (!debug_file->OpenFile(file_name.c_str(), false)) { if (!debug_file.is_open()) {
return nullptr; return nullptr;
} }
return absl::make_unique<AecDumpImpl>(std::move(debug_file), return absl::make_unique<AecDumpImpl>(std::move(debug_file),
@ -233,11 +229,7 @@ std::unique_ptr<AecDump> AecDumpFactory::Create(FILE* handle,
rtc::TaskQueue* worker_queue) { rtc::TaskQueue* worker_queue) {
RTC_DCHECK(worker_queue); RTC_DCHECK(worker_queue);
RTC_DCHECK(handle); RTC_DCHECK(handle);
std::unique_ptr<FileWrapper> debug_file(FileWrapper::Create()); return absl::make_unique<AecDumpImpl>(FileWrapper(handle), max_log_size_bytes,
if (!debug_file->OpenFromFileHandle(handle)) { worker_queue);
return nullptr;
}
return absl::make_unique<AecDumpImpl>(std::move(debug_file),
max_log_size_bytes, worker_queue);
} }
} // namespace webrtc } // namespace webrtc

View File

@ -46,7 +46,7 @@ namespace webrtc {
class AecDumpImpl : public AecDump { class AecDumpImpl : public AecDump {
public: public:
// Does member variables initialization shared across all c-tors. // Does member variables initialization shared across all c-tors.
AecDumpImpl(std::unique_ptr<FileWrapper> debug_file, AecDumpImpl(FileWrapper debug_file,
int64_t max_log_size_bytes, int64_t max_log_size_bytes,
rtc::TaskQueue* worker_queue); rtc::TaskQueue* worker_queue);
@ -73,7 +73,7 @@ class AecDumpImpl : public AecDump {
private: private:
std::unique_ptr<WriteToFileTask> CreateWriteToFileTask(); std::unique_ptr<WriteToFileTask> CreateWriteToFileTask();
std::unique_ptr<FileWrapper> debug_file_; FileWrapper debug_file_;
int64_t num_bytes_left_for_log_ = 0; int64_t num_bytes_left_for_log_ = 0;
rtc::RaceChecker race_checker_; rtc::RaceChecker race_checker_;
rtc::TaskQueue* worker_queue_; rtc::TaskQueue* worker_queue_;

View File

@ -39,17 +39,15 @@ void WriteToFileTask::UpdateBytesLeft(size_t event_byte_size) {
} }
bool WriteToFileTask::Run() { bool WriteToFileTask::Run() {
if (!debug_file_->is_open()) {
return true;
}
ProtoString event_string; ProtoString event_string;
event_.SerializeToString(&event_string); event_.SerializeToString(&event_string);
const size_t event_byte_size = event_.ByteSizeLong(); const size_t event_byte_size = event_.ByteSizeLong();
if (!IsRoomForNextEvent(event_byte_size)) { if (!IsRoomForNextEvent(event_byte_size)) {
debug_file_->CloseFile(); // Ensure that no further events are written, even if they're smaller than
// the current event.
*num_bytes_left_for_log_ = 0;
return true; return true;
} }

View File

@ -48,9 +48,9 @@ class WriteToFileTask : public rtc::QueuedTask {
bool Run() override; bool Run() override;
webrtc::FileWrapper* debug_file_; webrtc::FileWrapper* const debug_file_;
audioproc::Event event_; audioproc::Event event_;
int64_t* num_bytes_left_for_log_; int64_t* const num_bytes_left_for_log_;
}; };
} // namespace webrtc } // namespace webrtc

View File

@ -39,16 +39,14 @@ int main(int argc, char* argv[]) {
return 0; return 0;
} }
std::unique_ptr<FileWrapper> pcm_file(FileWrapper::Create()); FileWrapper pcm_file = FileWrapper::OpenReadOnly(argv[1]);
pcm_file->OpenFile(argv[1], true); if (!pcm_file.is_open()) {
if (!pcm_file->is_open()) {
printf("\nThe %s could not be opened.\n\n", argv[1]); printf("\nThe %s could not be opened.\n\n", argv[1]);
return -1; return -1;
} }
std::unique_ptr<FileWrapper> dat_file(FileWrapper::Create()); FileWrapper dat_file = FileWrapper::OpenWriteOnly(argv[2]);
dat_file->OpenFile(argv[2], false); if (!dat_file.is_open()) {
if (!dat_file->is_open()) {
printf("\nThe %s could not be opened.\n\n", argv[2]); printf("\nThe %s could not be opened.\n\n", argv[2]);
return -1; return -1;
} }
@ -73,7 +71,7 @@ int main(int argc, char* argv[]) {
// Read first buffer from the PCM test file. // Read first buffer from the PCM test file.
size_t file_samples_read = ReadInt16FromFileToFloatBuffer( size_t file_samples_read = ReadInt16FromFileToFloatBuffer(
pcm_file.get(), audio_buffer_length, audio_buffer.get()); &pcm_file, audio_buffer_length, audio_buffer.get());
for (int time = 0; file_samples_read > 0; time += chunk_size_ms) { for (int time = 0; file_samples_read > 0; time += chunk_size_ms) {
// Pad the rest of the buffer with zeros. // Pad the rest of the buffer with zeros.
for (size_t i = file_samples_read; i < audio_buffer_length; ++i) { for (size_t i = file_samples_read; i < audio_buffer_length; ++i) {
@ -91,19 +89,19 @@ int main(int argc, char* argv[]) {
// Read next buffer from the PCM test file. // Read next buffer from the PCM test file.
file_samples_read = ReadInt16FromFileToFloatBuffer( file_samples_read = ReadInt16FromFileToFloatBuffer(
pcm_file.get(), audio_buffer_length, audio_buffer.get()); &pcm_file, audio_buffer_length, audio_buffer.get());
} }
size_t floats_written = size_t floats_written =
WriteFloatBufferToFile(dat_file.get(), send_times.size(), &send_times[0]); WriteFloatBufferToFile(&dat_file, send_times.size(), &send_times[0]);
if (floats_written == 0) { if (floats_written == 0) {
printf("\nThe send times could not be written to DAT file\n\n"); printf("\nThe send times could not be written to DAT file\n\n");
return -1; return -1;
} }
pcm_file->CloseFile(); pcm_file.Close();
dat_file->CloseFile(); dat_file.Close();
return lost_packets; return lost_packets;
} }

View File

@ -158,22 +158,20 @@ TEST_F(TransientFileUtilsTest, MAYBE_ConvertDoubleToByteArray) {
TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16BufferFromFile) { TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16BufferFromFile) {
std::string test_filename = kTestFileName; std::string test_filename = kTestFileName;
std::unique_ptr<FileWrapper> file(FileWrapper::Create()); FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str());
ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
file->OpenFile(test_filename.c_str(), true); // Read only. << kTestFileName.c_str();
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n"
<< kTestFileName.c_str();
const size_t kBufferLength = 12; const size_t kBufferLength = 12;
std::unique_ptr<int16_t[]> buffer(new int16_t[kBufferLength]); std::unique_ptr<int16_t[]> buffer(new int16_t[kBufferLength]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadInt16BufferFromFile(file.get(), kBufferLength, buffer.get())); ReadInt16BufferFromFile(&file, kBufferLength, buffer.get()));
EXPECT_EQ(22377, buffer[4]); EXPECT_EQ(22377, buffer[4]);
EXPECT_EQ(16389, buffer[7]); EXPECT_EQ(16389, buffer[7]);
EXPECT_EQ(17631, buffer[kBufferLength - 1]); EXPECT_EQ(17631, buffer[kBufferLength - 1]);
file->Rewind(); file.Rewind();
// The next test is for checking the case where there are not as much data as // The next test is for checking the case where there are not as much data as
// needed in the file, but reads to the end, and it returns the number of // needed in the file, but reads to the end, and it returns the number of
@ -181,7 +179,7 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16BufferFromFile) {
const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; const size_t kBufferLenghtLargerThanFile = kBufferLength * 2;
buffer.reset(new int16_t[kBufferLenghtLargerThanFile]); buffer.reset(new int16_t[kBufferLenghtLargerThanFile]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadInt16BufferFromFile(file.get(), kBufferLenghtLargerThanFile, ReadInt16BufferFromFile(&file, kBufferLenghtLargerThanFile,
buffer.get())); buffer.get()));
EXPECT_EQ(11544, buffer[0]); EXPECT_EQ(11544, buffer[0]);
EXPECT_EQ(22377, buffer[4]); EXPECT_EQ(22377, buffer[4]);
@ -198,24 +196,22 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16BufferFromFile) {
TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToFloatBuffer) { TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToFloatBuffer) {
std::string test_filename = kTestFileName; std::string test_filename = kTestFileName;
std::unique_ptr<FileWrapper> file(FileWrapper::Create()); FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str());
ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
file->OpenFile(test_filename.c_str(), true); // Read only. << kTestFileName.c_str();
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n"
<< kTestFileName.c_str();
const size_t kBufferLength = 12; const size_t kBufferLength = 12;
std::unique_ptr<float[]> buffer(new float[kBufferLength]); std::unique_ptr<float[]> buffer(new float[kBufferLength]);
EXPECT_EQ(kBufferLength, ReadInt16FromFileToFloatBuffer( EXPECT_EQ(kBufferLength,
file.get(), kBufferLength, buffer.get())); ReadInt16FromFileToFloatBuffer(&file, kBufferLength, buffer.get()));
EXPECT_DOUBLE_EQ(11544, buffer[0]); EXPECT_DOUBLE_EQ(11544, buffer[0]);
EXPECT_DOUBLE_EQ(22377, buffer[4]); EXPECT_DOUBLE_EQ(22377, buffer[4]);
EXPECT_DOUBLE_EQ(16389, buffer[7]); EXPECT_DOUBLE_EQ(16389, buffer[7]);
EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]); EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]);
file->Rewind(); file.Rewind();
// The next test is for checking the case where there are not as much data as // The next test is for checking the case where there are not as much data as
// needed in the file, but reads to the end, and it returns the number of // needed in the file, but reads to the end, and it returns the number of
@ -223,8 +219,8 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToFloatBuffer) {
const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; const size_t kBufferLenghtLargerThanFile = kBufferLength * 2;
buffer.reset(new float[kBufferLenghtLargerThanFile]); buffer.reset(new float[kBufferLenghtLargerThanFile]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadInt16FromFileToFloatBuffer( ReadInt16FromFileToFloatBuffer(&file, kBufferLenghtLargerThanFile,
file.get(), kBufferLenghtLargerThanFile, buffer.get())); buffer.get()));
EXPECT_DOUBLE_EQ(11544, buffer[0]); EXPECT_DOUBLE_EQ(11544, buffer[0]);
EXPECT_DOUBLE_EQ(22377, buffer[4]); EXPECT_DOUBLE_EQ(22377, buffer[4]);
EXPECT_DOUBLE_EQ(16389, buffer[7]); EXPECT_DOUBLE_EQ(16389, buffer[7]);
@ -240,23 +236,21 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToFloatBuffer) {
TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToDoubleBuffer) { TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToDoubleBuffer) {
std::string test_filename = kTestFileName; std::string test_filename = kTestFileName;
std::unique_ptr<FileWrapper> file(FileWrapper::Create()); FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str());
ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
file->OpenFile(test_filename.c_str(), true); // Read only. << kTestFileName.c_str();
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n"
<< kTestFileName.c_str();
const size_t kBufferLength = 12; const size_t kBufferLength = 12;
std::unique_ptr<double[]> buffer(new double[kBufferLength]); std::unique_ptr<double[]> buffer(new double[kBufferLength]);
EXPECT_EQ(kBufferLength, ReadInt16FromFileToDoubleBuffer( EXPECT_EQ(kBufferLength, ReadInt16FromFileToDoubleBuffer(&file, kBufferLength,
file.get(), kBufferLength, buffer.get())); buffer.get()));
EXPECT_DOUBLE_EQ(11544, buffer[0]); EXPECT_DOUBLE_EQ(11544, buffer[0]);
EXPECT_DOUBLE_EQ(22377, buffer[4]); EXPECT_DOUBLE_EQ(22377, buffer[4]);
EXPECT_DOUBLE_EQ(16389, buffer[7]); EXPECT_DOUBLE_EQ(16389, buffer[7]);
EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]); EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]);
file->Rewind(); file.Rewind();
// The next test is for checking the case where there are not as much data as // The next test is for checking the case where there are not as much data as
// needed in the file, but reads to the end, and it returns the number of // needed in the file, but reads to the end, and it returns the number of
@ -264,8 +258,8 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToDoubleBuffer) {
const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; const size_t kBufferLenghtLargerThanFile = kBufferLength * 2;
buffer.reset(new double[kBufferLenghtLargerThanFile]); buffer.reset(new double[kBufferLenghtLargerThanFile]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadInt16FromFileToDoubleBuffer( ReadInt16FromFileToDoubleBuffer(&file, kBufferLenghtLargerThanFile,
file.get(), kBufferLenghtLargerThanFile, buffer.get())); buffer.get()));
EXPECT_DOUBLE_EQ(11544, buffer[0]); EXPECT_DOUBLE_EQ(11544, buffer[0]);
EXPECT_DOUBLE_EQ(22377, buffer[4]); EXPECT_DOUBLE_EQ(22377, buffer[4]);
EXPECT_DOUBLE_EQ(16389, buffer[7]); EXPECT_DOUBLE_EQ(16389, buffer[7]);
@ -280,22 +274,20 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToDoubleBuffer) {
TEST_F(TransientFileUtilsTest, MAYBE_ReadFloatBufferFromFile) { TEST_F(TransientFileUtilsTest, MAYBE_ReadFloatBufferFromFile) {
std::string test_filename = kTestFileNamef; std::string test_filename = kTestFileNamef;
std::unique_ptr<FileWrapper> file(FileWrapper::Create()); FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str());
ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
file->OpenFile(test_filename.c_str(), true); // Read only. << kTestFileNamef.c_str();
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n"
<< kTestFileNamef.c_str();
const size_t kBufferLength = 3; const size_t kBufferLength = 3;
std::unique_ptr<float[]> buffer(new float[kBufferLength]); std::unique_ptr<float[]> buffer(new float[kBufferLength]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadFloatBufferFromFile(file.get(), kBufferLength, buffer.get())); ReadFloatBufferFromFile(&file, kBufferLength, buffer.get()));
EXPECT_FLOAT_EQ(kPi, buffer[0]); EXPECT_FLOAT_EQ(kPi, buffer[0]);
EXPECT_FLOAT_EQ(kE, buffer[1]); EXPECT_FLOAT_EQ(kE, buffer[1]);
EXPECT_FLOAT_EQ(kAvogadro, buffer[2]); EXPECT_FLOAT_EQ(kAvogadro, buffer[2]);
file->Rewind(); file.Rewind();
// The next test is for checking the case where there are not as much data as // The next test is for checking the case where there are not as much data as
// needed in the file, but reads to the end, and it returns the number of // needed in the file, but reads to the end, and it returns the number of
@ -303,7 +295,7 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadFloatBufferFromFile) {
const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; const size_t kBufferLenghtLargerThanFile = kBufferLength * 2;
buffer.reset(new float[kBufferLenghtLargerThanFile]); buffer.reset(new float[kBufferLenghtLargerThanFile]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadFloatBufferFromFile(file.get(), kBufferLenghtLargerThanFile, ReadFloatBufferFromFile(&file, kBufferLenghtLargerThanFile,
buffer.get())); buffer.get()));
EXPECT_FLOAT_EQ(kPi, buffer[0]); EXPECT_FLOAT_EQ(kPi, buffer[0]);
EXPECT_FLOAT_EQ(kE, buffer[1]); EXPECT_FLOAT_EQ(kE, buffer[1]);
@ -318,22 +310,20 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadFloatBufferFromFile) {
TEST_F(TransientFileUtilsTest, MAYBE_ReadDoubleBufferFromFile) { TEST_F(TransientFileUtilsTest, MAYBE_ReadDoubleBufferFromFile) {
std::string test_filename = kTestFileName; std::string test_filename = kTestFileName;
std::unique_ptr<FileWrapper> file(FileWrapper::Create()); FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str());
ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
file->OpenFile(test_filename.c_str(), true); // Read only. << kTestFileName.c_str();
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n"
<< kTestFileName.c_str();
const size_t kBufferLength = 3; const size_t kBufferLength = 3;
std::unique_ptr<double[]> buffer(new double[kBufferLength]); std::unique_ptr<double[]> buffer(new double[kBufferLength]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadDoubleBufferFromFile(file.get(), kBufferLength, buffer.get())); ReadDoubleBufferFromFile(&file, kBufferLength, buffer.get()));
EXPECT_DOUBLE_EQ(kPi, buffer[0]); EXPECT_DOUBLE_EQ(kPi, buffer[0]);
EXPECT_DOUBLE_EQ(kE, buffer[1]); EXPECT_DOUBLE_EQ(kE, buffer[1]);
EXPECT_DOUBLE_EQ(kAvogadro, buffer[2]); EXPECT_DOUBLE_EQ(kAvogadro, buffer[2]);
file->Rewind(); file.Rewind();
// The next test is for checking the case where there are not as much data as // The next test is for checking the case where there are not as much data as
// needed in the file, but reads to the end, and it returns the number of // needed in the file, but reads to the end, and it returns the number of
@ -341,7 +331,7 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadDoubleBufferFromFile) {
const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; const size_t kBufferLenghtLargerThanFile = kBufferLength * 2;
buffer.reset(new double[kBufferLenghtLargerThanFile]); buffer.reset(new double[kBufferLenghtLargerThanFile]);
EXPECT_EQ(kBufferLength, EXPECT_EQ(kBufferLength,
ReadDoubleBufferFromFile(file.get(), kBufferLenghtLargerThanFile, ReadDoubleBufferFromFile(&file, kBufferLenghtLargerThanFile,
buffer.get())); buffer.get()));
EXPECT_DOUBLE_EQ(kPi, buffer[0]); EXPECT_DOUBLE_EQ(kPi, buffer[0]);
EXPECT_DOUBLE_EQ(kE, buffer[1]); EXPECT_DOUBLE_EQ(kE, buffer[1]);
@ -354,14 +344,12 @@ TEST_F(TransientFileUtilsTest, MAYBE_ReadDoubleBufferFromFile) {
#define MAYBE_WriteInt16BufferToFile WriteInt16BufferToFile #define MAYBE_WriteInt16BufferToFile WriteInt16BufferToFile
#endif #endif
TEST_F(TransientFileUtilsTest, MAYBE_WriteInt16BufferToFile) { TEST_F(TransientFileUtilsTest, MAYBE_WriteInt16BufferToFile) {
std::unique_ptr<FileWrapper> file(FileWrapper::Create());
std::string kOutFileName = std::string kOutFileName =
CreateTempFilename(test::OutputPath(), "utils_test"); CreateTempFilename(test::OutputPath(), "utils_test");
file->OpenFile(kOutFileName.c_str(), false); // Write mode. FileWrapper file = FileWrapper::OpenWriteOnly(kOutFileName.c_str());
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n" ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
<< kOutFileName.c_str(); << kOutFileName.c_str();
const size_t kBufferLength = 3; const size_t kBufferLength = 3;
std::unique_ptr<int16_t[]> written_buffer(new int16_t[kBufferLength]); std::unique_ptr<int16_t[]> written_buffer(new int16_t[kBufferLength]);
@ -371,17 +359,17 @@ TEST_F(TransientFileUtilsTest, MAYBE_WriteInt16BufferToFile) {
written_buffer[1] = 2; written_buffer[1] = 2;
written_buffer[2] = 3; written_buffer[2] = 3;
EXPECT_EQ(kBufferLength, WriteInt16BufferToFile(file.get(), kBufferLength, EXPECT_EQ(kBufferLength,
written_buffer.get())); WriteInt16BufferToFile(&file, kBufferLength, written_buffer.get()));
file->CloseFile(); file.Close();
file->OpenFile(kOutFileName.c_str(), true); // Read only. file = FileWrapper::OpenReadOnly(kOutFileName.c_str());
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n" ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
<< kOutFileName.c_str(); << kOutFileName.c_str();
EXPECT_EQ(kBufferLength, ReadInt16BufferFromFile(file.get(), kBufferLength, EXPECT_EQ(kBufferLength,
read_buffer.get())); ReadInt16BufferFromFile(&file, kBufferLength, read_buffer.get()));
EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(), EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(),
kBufferLength * sizeof(written_buffer[0]))); kBufferLength * sizeof(written_buffer[0])));
} }
@ -392,14 +380,12 @@ TEST_F(TransientFileUtilsTest, MAYBE_WriteInt16BufferToFile) {
#define MAYBE_WriteFloatBufferToFile WriteFloatBufferToFile #define MAYBE_WriteFloatBufferToFile WriteFloatBufferToFile
#endif #endif
TEST_F(TransientFileUtilsTest, MAYBE_WriteFloatBufferToFile) { TEST_F(TransientFileUtilsTest, MAYBE_WriteFloatBufferToFile) {
std::unique_ptr<FileWrapper> file(FileWrapper::Create());
std::string kOutFileName = std::string kOutFileName =
CreateTempFilename(test::OutputPath(), "utils_test"); CreateTempFilename(test::OutputPath(), "utils_test");
file->OpenFile(kOutFileName.c_str(), false); // Write mode. FileWrapper file = FileWrapper::OpenWriteOnly(kOutFileName.c_str());
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n" ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
<< kOutFileName.c_str(); << kOutFileName.c_str();
const size_t kBufferLength = 3; const size_t kBufferLength = 3;
std::unique_ptr<float[]> written_buffer(new float[kBufferLength]); std::unique_ptr<float[]> written_buffer(new float[kBufferLength]);
@ -409,17 +395,17 @@ TEST_F(TransientFileUtilsTest, MAYBE_WriteFloatBufferToFile) {
written_buffer[1] = static_cast<float>(kE); written_buffer[1] = static_cast<float>(kE);
written_buffer[2] = static_cast<float>(kAvogadro); written_buffer[2] = static_cast<float>(kAvogadro);
EXPECT_EQ(kBufferLength, WriteFloatBufferToFile(file.get(), kBufferLength, EXPECT_EQ(kBufferLength,
written_buffer.get())); WriteFloatBufferToFile(&file, kBufferLength, written_buffer.get()));
file->CloseFile(); file.Close();
file->OpenFile(kOutFileName.c_str(), true); // Read only. file = FileWrapper::OpenReadOnly(kOutFileName.c_str());
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n" ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
<< kOutFileName.c_str(); << kOutFileName.c_str();
EXPECT_EQ(kBufferLength, ReadFloatBufferFromFile(file.get(), kBufferLength, EXPECT_EQ(kBufferLength,
read_buffer.get())); ReadFloatBufferFromFile(&file, kBufferLength, read_buffer.get()));
EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(), EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(),
kBufferLength * sizeof(written_buffer[0]))); kBufferLength * sizeof(written_buffer[0])));
} }
@ -430,14 +416,12 @@ TEST_F(TransientFileUtilsTest, MAYBE_WriteFloatBufferToFile) {
#define MAYBE_WriteDoubleBufferToFile WriteDoubleBufferToFile #define MAYBE_WriteDoubleBufferToFile WriteDoubleBufferToFile
#endif #endif
TEST_F(TransientFileUtilsTest, MAYBE_WriteDoubleBufferToFile) { TEST_F(TransientFileUtilsTest, MAYBE_WriteDoubleBufferToFile) {
std::unique_ptr<FileWrapper> file(FileWrapper::Create());
std::string kOutFileName = std::string kOutFileName =
CreateTempFilename(test::OutputPath(), "utils_test"); CreateTempFilename(test::OutputPath(), "utils_test");
file->OpenFile(kOutFileName.c_str(), false); // Write mode. FileWrapper file = FileWrapper::OpenWriteOnly(kOutFileName.c_str());
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n" ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
<< kOutFileName.c_str(); << kOutFileName.c_str();
const size_t kBufferLength = 3; const size_t kBufferLength = 3;
std::unique_ptr<double[]> written_buffer(new double[kBufferLength]); std::unique_ptr<double[]> written_buffer(new double[kBufferLength]);
@ -447,17 +431,17 @@ TEST_F(TransientFileUtilsTest, MAYBE_WriteDoubleBufferToFile) {
written_buffer[1] = kE; written_buffer[1] = kE;
written_buffer[2] = kAvogadro; written_buffer[2] = kAvogadro;
EXPECT_EQ(kBufferLength, WriteDoubleBufferToFile(file.get(), kBufferLength, EXPECT_EQ(kBufferLength, WriteDoubleBufferToFile(&file, kBufferLength,
written_buffer.get())); written_buffer.get()));
file->CloseFile(); file.Close();
file->OpenFile(kOutFileName.c_str(), true); // Read only. file = FileWrapper::OpenReadOnly(kOutFileName.c_str());
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n" ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
<< kOutFileName.c_str(); << kOutFileName.c_str();
EXPECT_EQ(kBufferLength, ReadDoubleBufferFromFile(file.get(), kBufferLength, EXPECT_EQ(kBufferLength,
read_buffer.get())); ReadDoubleBufferFromFile(&file, kBufferLength, read_buffer.get()));
EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(), EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(),
kBufferLength * sizeof(written_buffer[0]))); kBufferLength * sizeof(written_buffer[0])));
} }
@ -473,7 +457,7 @@ TEST_F(TransientFileUtilsTest, MAYBE_ExpectedErrorReturnValues) {
double value; double value;
std::unique_ptr<int16_t[]> int16_buffer(new int16_t[1]); std::unique_ptr<int16_t[]> int16_buffer(new int16_t[1]);
std::unique_ptr<double[]> double_buffer(new double[1]); std::unique_ptr<double[]> double_buffer(new double[1]);
std::unique_ptr<FileWrapper> file(FileWrapper::Create()); FileWrapper file;
EXPECT_EQ(-1, ConvertByteArrayToDouble(NULL, &value)); EXPECT_EQ(-1, ConvertByteArrayToDouble(NULL, &value));
EXPECT_EQ(-1, ConvertByteArrayToDouble(kPiBytes, NULL)); EXPECT_EQ(-1, ConvertByteArrayToDouble(kPiBytes, NULL));
@ -481,37 +465,35 @@ TEST_F(TransientFileUtilsTest, MAYBE_ExpectedErrorReturnValues) {
EXPECT_EQ(-1, ConvertDoubleToByteArray(kPi, NULL)); EXPECT_EQ(-1, ConvertDoubleToByteArray(kPi, NULL));
// Tests with file not opened. // Tests with file not opened.
EXPECT_EQ(0u, ReadInt16BufferFromFile(file.get(), 1, int16_buffer.get())); EXPECT_EQ(0u, ReadInt16BufferFromFile(&file, 1, int16_buffer.get()));
EXPECT_EQ( EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(&file, 1, double_buffer.get()));
0u, ReadInt16FromFileToDoubleBuffer(file.get(), 1, double_buffer.get())); EXPECT_EQ(0u, ReadDoubleBufferFromFile(&file, 1, double_buffer.get()));
EXPECT_EQ(0u, ReadDoubleBufferFromFile(file.get(), 1, double_buffer.get())); EXPECT_EQ(0u, WriteInt16BufferToFile(&file, 1, int16_buffer.get()));
EXPECT_EQ(0u, WriteInt16BufferToFile(file.get(), 1, int16_buffer.get())); EXPECT_EQ(0u, WriteDoubleBufferToFile(&file, 1, double_buffer.get()));
EXPECT_EQ(0u, WriteDoubleBufferToFile(file.get(), 1, double_buffer.get()));
file->OpenFile(test_filename.c_str(), true); // Read only. file = FileWrapper::OpenReadOnly(test_filename.c_str());
ASSERT_TRUE(file->is_open()) << "File could not be opened:\n" ASSERT_TRUE(file.is_open()) << "File could not be opened:\n"
<< kTestFileName.c_str(); << kTestFileName.c_str();
EXPECT_EQ(0u, ReadInt16BufferFromFile(NULL, 1, int16_buffer.get())); EXPECT_EQ(0u, ReadInt16BufferFromFile(NULL, 1, int16_buffer.get()));
EXPECT_EQ(0u, ReadInt16BufferFromFile(file.get(), 1, NULL)); EXPECT_EQ(0u, ReadInt16BufferFromFile(&file, 1, NULL));
EXPECT_EQ(0u, ReadInt16BufferFromFile(file.get(), 0, int16_buffer.get())); EXPECT_EQ(0u, ReadInt16BufferFromFile(&file, 0, int16_buffer.get()));
EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(NULL, 1, double_buffer.get())); EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(NULL, 1, double_buffer.get()));
EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(file.get(), 1, NULL)); EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(&file, 1, NULL));
EXPECT_EQ( EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(&file, 0, double_buffer.get()));
0u, ReadInt16FromFileToDoubleBuffer(file.get(), 0, double_buffer.get()));
EXPECT_EQ(0u, ReadDoubleBufferFromFile(NULL, 1, double_buffer.get())); EXPECT_EQ(0u, ReadDoubleBufferFromFile(NULL, 1, double_buffer.get()));
EXPECT_EQ(0u, ReadDoubleBufferFromFile(file.get(), 1, NULL)); EXPECT_EQ(0u, ReadDoubleBufferFromFile(&file, 1, NULL));
EXPECT_EQ(0u, ReadDoubleBufferFromFile(file.get(), 0, double_buffer.get())); EXPECT_EQ(0u, ReadDoubleBufferFromFile(&file, 0, double_buffer.get()));
EXPECT_EQ(0u, WriteInt16BufferToFile(NULL, 1, int16_buffer.get())); EXPECT_EQ(0u, WriteInt16BufferToFile(NULL, 1, int16_buffer.get()));
EXPECT_EQ(0u, WriteInt16BufferToFile(file.get(), 1, NULL)); EXPECT_EQ(0u, WriteInt16BufferToFile(&file, 1, NULL));
EXPECT_EQ(0u, WriteInt16BufferToFile(file.get(), 0, int16_buffer.get())); EXPECT_EQ(0u, WriteInt16BufferToFile(&file, 0, int16_buffer.get()));
EXPECT_EQ(0u, WriteDoubleBufferToFile(NULL, 1, double_buffer.get())); EXPECT_EQ(0u, WriteDoubleBufferToFile(NULL, 1, double_buffer.get()));
EXPECT_EQ(0u, WriteDoubleBufferToFile(file.get(), 1, NULL)); EXPECT_EQ(0u, WriteDoubleBufferToFile(&file, 1, NULL));
EXPECT_EQ(0u, WriteDoubleBufferToFile(file.get(), 0, double_buffer.get())); EXPECT_EQ(0u, WriteDoubleBufferToFile(&file, 0, double_buffer.get()));
} }
} // namespace webrtc } // namespace webrtc

View File

@ -47,13 +47,10 @@ TEST(TransientDetectorTest, CorrectnessBasedOnFiles) {
detect_file_name << "audio_processing/transient/detect" detect_file_name << "audio_processing/transient/detect"
<< (sample_rate_hz / 1000) << "kHz"; << (sample_rate_hz / 1000) << "kHz";
std::unique_ptr<FileWrapper> detect_file(FileWrapper::Create()); FileWrapper detect_file = FileWrapper::OpenReadOnly(
test::ResourcePath(detect_file_name.str(), "dat").c_str());
detect_file->OpenFile( bool file_opened = detect_file.is_open();
test::ResourcePath(detect_file_name.str(), "dat").c_str(),
true); // Read only.
bool file_opened = detect_file->is_open();
ASSERT_TRUE(file_opened) << "File could not be opened.\n" ASSERT_TRUE(file_opened) << "File could not be opened.\n"
<< detect_file_name.str().c_str(); << detect_file_name.str().c_str();
@ -62,11 +59,8 @@ TEST(TransientDetectorTest, CorrectnessBasedOnFiles) {
audio_file_name << "audio_processing/transient/audio" audio_file_name << "audio_processing/transient/audio"
<< (sample_rate_hz / 1000) << "kHz"; << (sample_rate_hz / 1000) << "kHz";
std::unique_ptr<FileWrapper> audio_file(FileWrapper::Create()); FileWrapper audio_file = FileWrapper::OpenReadOnly(
test::ResourcePath(audio_file_name.str(), "pcm").c_str());
audio_file->OpenFile(
test::ResourcePath(audio_file_name.str(), "pcm").c_str(),
true); // Read only.
// Create detector. // Create detector.
TransientDetector detector(sample_rate_hz); TransientDetector detector(sample_rate_hz);
@ -78,14 +72,14 @@ TEST(TransientDetectorTest, CorrectnessBasedOnFiles) {
size_t frames_read = 0; size_t frames_read = 0;
while (ReadInt16FromFileToFloatBuffer(audio_file.get(), buffer_length, while (ReadInt16FromFileToFloatBuffer(&audio_file, buffer_length,
buffer.get()) == buffer_length) { buffer.get()) == buffer_length) {
++frames_read; ++frames_read;
float detector_value = float detector_value =
detector.Detect(buffer.get(), buffer_length, NULL, 0); detector.Detect(buffer.get(), buffer_length, NULL, 0);
double file_value; double file_value;
ASSERT_EQ(1u, ReadDoubleBufferFromFile(detect_file.get(), 1, &file_value)) ASSERT_EQ(1u, ReadDoubleBufferFromFile(&detect_file, 1, &file_value))
<< "Detect test file is malformed.\n"; << "Detect test file is malformed.\n";
// Compare results with data from the matlab test file. // Compare results with data from the matlab test file.
@ -93,8 +87,8 @@ TEST(TransientDetectorTest, CorrectnessBasedOnFiles) {
<< "Frame: " << frames_read; << "Frame: " << frames_read;
} }
detect_file->CloseFile(); detect_file.Close();
audio_file->CloseFile(); audio_file.Close();
} }
} }

View File

@ -80,31 +80,27 @@ TEST(WPDTreeTest, CorrectnessBasedOnMatlabFiles) {
kDaubechies8LowPassCoefficients, kDaubechies8CoefficientsLength, kDaubechies8LowPassCoefficients, kDaubechies8CoefficientsLength,
kLevels); kLevels);
// Allocate and open all matlab and out files. // Allocate and open all matlab and out files.
std::unique_ptr<FileWrapper> matlab_files_data[kLeaves]; FileWrapper matlab_files_data[kLeaves];
std::unique_ptr<FileWrapper> out_files_data[kLeaves]; FileWrapper out_files_data[kLeaves];
for (int i = 0; i < kLeaves; ++i) { for (int i = 0; i < kLeaves; ++i) {
// Matlab files. // Matlab files.
matlab_files_data[i].reset(FileWrapper::Create());
rtc::StringBuilder matlab_stream; rtc::StringBuilder matlab_stream;
matlab_stream << "audio_processing/transient/wpd" << i; matlab_stream << "audio_processing/transient/wpd" << i;
std::string matlab_string = test::ResourcePath(matlab_stream.str(), "dat"); std::string matlab_string = test::ResourcePath(matlab_stream.str(), "dat");
matlab_files_data[i]->OpenFile(matlab_string.c_str(), true); // Read only. matlab_files_data[i] = FileWrapper::OpenReadOnly(matlab_string.c_str());
bool file_opened = matlab_files_data[i]->is_open(); bool file_opened = matlab_files_data[i].is_open();
ASSERT_TRUE(file_opened) << "File could not be opened.\n" << matlab_string; ASSERT_TRUE(file_opened) << "File could not be opened.\n" << matlab_string;
// Out files. // Out files.
out_files_data[i].reset(FileWrapper::Create());
rtc::StringBuilder out_stream; rtc::StringBuilder out_stream;
out_stream << test::OutputPath() << "wpd_" << i << ".out"; out_stream << test::OutputPath() << "wpd_" << i << ".out";
std::string out_string = out_stream.str(); std::string out_string = out_stream.str();
out_files_data[i]->OpenFile(out_string.c_str(), false); // Write mode. out_files_data[i] = FileWrapper::OpenWriteOnly(out_string.c_str());
file_opened = out_files_data[i]->is_open(); file_opened = out_files_data[i].is_open();
ASSERT_TRUE(file_opened) << "File could not be opened.\n" << out_string; ASSERT_TRUE(file_opened) << "File could not be opened.\n" << out_string;
} }
@ -112,11 +108,9 @@ TEST(WPDTreeTest, CorrectnessBasedOnMatlabFiles) {
std::string test_file_name = test::ResourcePath( std::string test_file_name = test::ResourcePath(
"audio_processing/transient/ajm-macbook-1-spke16m", "pcm"); "audio_processing/transient/ajm-macbook-1-spke16m", "pcm");
std::unique_ptr<FileWrapper> test_file(FileWrapper::Create()); FileWrapper test_file = FileWrapper::OpenReadOnly(test_file_name.c_str());
test_file->OpenFile(test_file_name.c_str(), true); // Read only. bool file_opened = test_file.is_open();
bool file_opened = test_file->is_open();
ASSERT_TRUE(file_opened) << "File could not be opened.\n" << test_file_name; ASSERT_TRUE(file_opened) << "File could not be opened.\n" << test_file_name;
float test_buffer[kTestBufferSize]; float test_buffer[kTestBufferSize];
@ -129,8 +123,8 @@ TEST(WPDTreeTest, CorrectnessBasedOnMatlabFiles) {
size_t frames_read = 0; size_t frames_read = 0;
// Read first buffer from the PCM test file. // Read first buffer from the PCM test file.
size_t file_samples_read = ReadInt16FromFileToFloatBuffer( size_t file_samples_read =
test_file.get(), kTestBufferSize, test_buffer); ReadInt16FromFileToFloatBuffer(&test_file, kTestBufferSize, test_buffer);
while (file_samples_read > 0 && frames_read < kMaxFramesToTest) { while (file_samples_read > 0 && frames_read < kMaxFramesToTest) {
++frames_read; ++frames_read;
@ -147,7 +141,7 @@ TEST(WPDTreeTest, CorrectnessBasedOnMatlabFiles) {
for (int i = 0; i < kLeaves; ++i) { for (int i = 0; i < kLeaves; ++i) {
// Compare data values // Compare data values
size_t matlab_samples_read = ReadDoubleBufferFromFile( size_t matlab_samples_read = ReadDoubleBufferFromFile(
matlab_files_data[i].get(), kLeavesSamples, matlab_buffer); &matlab_files_data[i], kLeavesSamples, matlab_buffer);
ASSERT_EQ(kLeavesSamples, matlab_samples_read) ASSERT_EQ(kLeavesSamples, matlab_samples_read)
<< "Matlab test files are malformed.\n" << "Matlab test files are malformed.\n"
@ -162,22 +156,21 @@ TEST(WPDTreeTest, CorrectnessBasedOnMatlabFiles) {
} }
// Write results to out files. // Write results to out files.
WriteFloatBufferToFile(out_files_data[i].get(), kLeavesSamples, WriteFloatBufferToFile(&out_files_data[i], kLeavesSamples, node_data);
node_data);
} }
// Read next buffer from the PCM test file. // Read next buffer from the PCM test file.
file_samples_read = ReadInt16FromFileToFloatBuffer( file_samples_read = ReadInt16FromFileToFloatBuffer(
test_file.get(), kTestBufferSize, test_buffer); &test_file, kTestBufferSize, test_buffer);
} }
// Close all matlab and out files. // Close all matlab and out files.
for (int i = 0; i < kLeaves; ++i) { for (int i = 0; i < kLeaves; ++i) {
matlab_files_data[i]->CloseFile(); matlab_files_data[i].Close();
out_files_data[i]->CloseFile(); out_files_data[i].Close();
} }
test_file->CloseFile(); test_file.Close();
} }
} // namespace webrtc } // namespace webrtc

View File

@ -34,22 +34,13 @@ FILE* FileOpen(const char* file_name_utf8, bool read_only) {
} // namespace } // namespace
// static // static
FileWrapper* FileWrapper::Create() { FileWrapper FileWrapper::OpenReadOnly(const char* file_name_utf8) {
return new FileWrapper(); return FileWrapper(FileOpen(file_name_utf8, true));
} }
// static // static
FileWrapper FileWrapper::Open(const char* file_name_utf8, bool read_only) { FileWrapper FileWrapper::OpenWriteOnly(const char* file_name_utf8) {
return FileWrapper(FileOpen(file_name_utf8, read_only), 0); return FileWrapper(FileOpen(file_name_utf8, false));
}
FileWrapper::FileWrapper() {}
FileWrapper::FileWrapper(FILE* file, size_t max_size)
: file_(file), max_size_in_bytes_(max_size) {}
FileWrapper::~FileWrapper() {
CloseFileImpl();
} }
FileWrapper::FileWrapper(FileWrapper&& other) { FileWrapper::FileWrapper(FileWrapper&& other) {
@ -57,95 +48,39 @@ FileWrapper::FileWrapper(FileWrapper&& other) {
} }
FileWrapper& FileWrapper::operator=(FileWrapper&& other) { FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
Close();
file_ = other.file_; file_ = other.file_;
max_size_in_bytes_ = other.max_size_in_bytes_;
position_ = other.position_;
other.file_ = nullptr; other.file_ = nullptr;
return *this; return *this;
} }
void FileWrapper::CloseFile() { bool FileWrapper::Rewind() {
rtc::CritScope lock(&lock_); RTC_DCHECK(file_);
CloseFileImpl(); return fseek(file_, 0, SEEK_SET) == 0;
} }
int FileWrapper::Rewind() { bool FileWrapper::Flush() {
rtc::CritScope lock(&lock_); RTC_DCHECK(file_);
if (file_ != nullptr) { return fflush(file_) == 0;
position_ = 0;
return fseek(file_, 0, SEEK_SET);
}
return -1;
} }
void FileWrapper::SetMaxFileSize(size_t bytes) { size_t FileWrapper::Read(void* buf, size_t length) {
rtc::CritScope lock(&lock_); RTC_DCHECK(file_);
max_size_in_bytes_ = bytes; return fread(buf, 1, length, file_);
}
int FileWrapper::Flush() {
rtc::CritScope lock(&lock_);
return FlushImpl();
}
bool FileWrapper::OpenFile(const char* file_name_utf8, bool read_only) {
size_t length = strlen(file_name_utf8);
if (length > kMaxFileNameSize - 1)
return false;
rtc::CritScope lock(&lock_);
if (file_ != nullptr)
return false;
file_ = FileOpen(file_name_utf8, read_only);
return file_ != nullptr;
}
bool FileWrapper::OpenFromFileHandle(FILE* handle) {
if (!handle)
return false;
rtc::CritScope lock(&lock_);
CloseFileImpl();
file_ = handle;
return true;
}
int FileWrapper::Read(void* buf, size_t length) {
rtc::CritScope lock(&lock_);
if (file_ == nullptr)
return -1;
size_t bytes_read = fread(buf, 1, length, file_);
return static_cast<int>(bytes_read);
} }
bool FileWrapper::Write(const void* buf, size_t length) { bool FileWrapper::Write(const void* buf, size_t length) {
if (buf == nullptr) RTC_DCHECK(file_);
return false; return fwrite(buf, 1, length, file_) == length;
}
rtc::CritScope lock(&lock_);
bool FileWrapper::Close() {
if (file_ == nullptr) if (file_ == nullptr)
return false; return true;
// Check if it's time to stop writing. bool success = fclose(file_) == 0;
if (max_size_in_bytes_ > 0 && (position_ + length) > max_size_in_bytes_)
return false;
size_t num_bytes = fwrite(buf, 1, length, file_);
position_ += num_bytes;
return num_bytes == length;
}
void FileWrapper::CloseFileImpl() {
if (file_ != nullptr)
fclose(file_);
file_ = nullptr; file_ = nullptr;
} return success;
int FileWrapper::FlushImpl() {
return (file_ != nullptr) ? fflush(file_) : -1;
} }
} // namespace webrtc } // namespace webrtc

View File

@ -20,64 +20,57 @@
namespace webrtc { namespace webrtc {
// TODO(tommi): Rename to rtc::File and move to base.
class FileWrapper final { class FileWrapper final {
public: public:
static const size_t kMaxFileNameSize = 1024; // Opens a file, in read or write mode. Use the is_open() method on the
// returned object to check if the open operation was successful. The file is
// closed by the destructor.
static FileWrapper OpenReadOnly(const char* file_name_utf8);
static FileWrapper OpenWriteOnly(const char* file_name_utf8);
// Factory methods. FileWrapper() = default;
// TODO(tommi): Remove Create().
static FileWrapper* Create();
static FileWrapper Open(const char* file_name_utf8, bool read_only);
FileWrapper(FILE* file, size_t max_size); // Takes over ownership of |file|, closing it on destruction.
~FileWrapper(); explicit FileWrapper(FILE* file) : file_(file) {}
~FileWrapper() { Close(); }
// Support for move semantics.
FileWrapper(FileWrapper&& other);
FileWrapper& operator=(FileWrapper&& other);
// Returns true if a file has been opened.
bool is_open() const { return file_ != nullptr; }
// Opens a file in read or write mode, decided by the read_only parameter.
bool OpenFile(const char* file_name_utf8, bool read_only);
// Initializes the wrapper from an existing handle. The wrapper
// takes ownership of |handle| and closes it in CloseFile().
bool OpenFromFileHandle(FILE* handle);
void CloseFile();
// Limits the file size to |bytes|. Writing will fail after the cap
// is hit. Pass zero to use an unlimited size.
// TODO(tommi): Could we move this out into a separate class?
void SetMaxFileSize(size_t bytes);
// Flush any pending writes. Note: Flushing when closing, is not required.
int Flush();
// Rewinds the file to the start.
int Rewind();
int Read(void* buf, size_t length);
bool Write(const void* buf, size_t length);
private:
FileWrapper();
void CloseFileImpl();
int FlushImpl();
// TODO(tommi): Remove the lock.
rtc::CriticalSection lock_;
FILE* file_ = nullptr;
size_t position_ = 0;
size_t max_size_in_bytes_ = 0;
// Copying is not supported. // Copying is not supported.
FileWrapper(const FileWrapper&) = delete; FileWrapper(const FileWrapper&) = delete;
FileWrapper& operator=(const FileWrapper&) = delete; FileWrapper& operator=(const FileWrapper&) = delete;
// Support for move semantics.
FileWrapper(FileWrapper&&);
FileWrapper& operator=(FileWrapper&&);
// Returns true if a file has been opened. If the file is not open, no methods
// but is_open and Close may be called.
bool is_open() const { return file_ != nullptr; }
// Closes the file, and implies Flush. Returns true on success, false if
// writing buffered data fails. On failure, the file is nevertheless closed.
// Calling Close on an already closed file does nothing and returns success.
bool Close();
// Write any buffered data to the underlying file. Returns true on success,
// false on write error. Note: Flushing when closing, is not required.
// TODO(nisse): Delete this method.
bool Flush();
// Seeks to the beginning of file. Returns true on success, false on failure,
// e.g., if the underlying file isn't seekable.
// TODO(nisse): Delete this method.
bool Rewind();
// Returns number of bytes read. Short count indicates EOF or error.
size_t Read(void* buf, size_t length);
// Returns true if all data was successfully written (or buffered), or false
// if there was an error. Writing buffered data can fail later, and is
// reported with return value from Flush or Close.
bool Write(const void* buf, size_t length);
private:
FILE* file_ = nullptr;
}; };
} // namespace webrtc } // namespace webrtc