diff --git a/iamf/cli/tests/wav_reader_test.cc b/iamf/cli/tests/wav_reader_test.cc index 7be5bc81..cbad80e3 100644 --- a/iamf/cli/tests/wav_reader_test.cc +++ b/iamf/cli/tests/wav_reader_test.cc @@ -13,8 +13,10 @@ #include #include +#include #include #include +#include #include // Placeholder for get runfiles header. @@ -168,5 +170,37 @@ TEST(WavReader, OneFrame32BitLittleEndian) { EXPECT_EQ(wav_reader.buffers_, expected_frame); } +TEST(WavReader, IsSafeToCallReadFrameAfterMove) { + const size_t kNumSamplesPerFrame = 1; + auto wav_reader = + InitAndValidate("stereo_8_samples_48khz_s16le.wav", kNumSamplesPerFrame); + auto wav_reader_moved = std::move(wav_reader); + + EXPECT_EQ(wav_reader_moved.ReadFrame(), 2); + const std::vector> kExpectedFrame = { + {0x00010000, static_cast(0xffff0000)}}; + EXPECT_EQ(wav_reader_moved.buffers_, kExpectedFrame); +} + +template +std::vector GetRawBytes(const T& object) { + std::vector raw_object(sizeof(object)); + std::memcpy(raw_object.data(), static_cast(&object), + raw_object.size()); + return raw_object; +} + +TEST(WavReader, IsByteEquivalentAfterMoving) { + const size_t kNumSamplesPerFrame = 1; + auto wav_reader = + InitAndValidate("stereo_8_samples_48khz_s16le.wav", kNumSamplesPerFrame); + const auto raw_wav_reader_before_move = GetRawBytes(wav_reader); + + const auto wav_reader_moved = std::move(wav_reader); + const auto raw_wav_reader_after_move = GetRawBytes(wav_reader_moved); + + EXPECT_EQ(raw_wav_reader_before_move, raw_wav_reader_after_move); +} + } // namespace } // namespace iamf_tools diff --git a/iamf/cli/wav_reader.cc b/iamf/cli/wav_reader.cc index f992af67..fc792c89 100644 --- a/iamf/cli/wav_reader.cc +++ b/iamf/cli/wav_reader.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "absl/log/check.h" @@ -54,6 +55,16 @@ WavReader::WavReader(const std::string& wav_filename, } } +WavReader::WavReader(WavReader&& original) + : buffers_(std::move(original.buffers_)), + num_samples_per_frame_(original.num_samples_per_frame_), + file_(original.file_), + info_(original.info_) { + // Invalidate the file pointer on the original copy to prevent it from being + // closed on destruction. + original.file_ = nullptr; +} + WavReader::~WavReader() { if (file_ != nullptr) { std::fclose(file_); diff --git a/iamf/cli/wav_reader.h b/iamf/cli/wav_reader.h index 7e7bc96d..ebe51743 100644 --- a/iamf/cli/wav_reader.h +++ b/iamf/cli/wav_reader.h @@ -32,6 +32,9 @@ class WavReader { */ WavReader(const std::string& wav_filename, size_t num_samples_per_frame); + /*!\brief Moves the `WavReader` without closing the underlying file.*/ + WavReader(WavReader&& original); + /*!\brief Destructor. */ ~WavReader();