Chromium Code Reviews| Index: webrtc/modules/video_coding/utility/ivf_file_writer_unittest.cc |
| diff --git a/webrtc/modules/video_coding/utility/ivf_file_writer_unittest.cc b/webrtc/modules/video_coding/utility/ivf_file_writer_unittest.cc |
| index f22aff78cd1c6957cdff010179adfcdc6d68b4f8..c65bc461389237d61172d427663dd8aeb45560dd 100644 |
| --- a/webrtc/modules/video_coding/utility/ivf_file_writer_unittest.cc |
| +++ b/webrtc/modules/video_coding/utility/ivf_file_writer_unittest.cc |
| @@ -20,29 +20,38 @@ |
| #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| #include "webrtc/test/testsupport/fileutils.h" |
| +#if defined(WEBRTC_WIN) |
| +#include "webrtc/base/win32.h" |
| +#else |
| +#include <unistd.h> |
| +#endif |
| + |
| namespace webrtc { |
| namespace { |
| static const int kHeaderSize = 32; |
| static const int kFrameHeaderSize = 12; |
| static uint8_t dummy_payload[4] = {0, 1, 2, 3}; |
| -static const int kMaxFileRetries = 5; |
| } // namespace |
| +void RemoveFile(const std::string& path) { |
| +#if defined(WEBRTC_WIN) |
| + ::DeleteFile(rtc::ToUtf16(path).c_str()); |
| +#else |
| + ::unlink(path.c_str()); |
| +#endif |
| +} |
| + |
| class IvfFileWriterTest : public ::testing::Test { |
| protected: |
| void SetUp() override { |
| - const uint64_t start_id = rtc::CreateRandomId64(); |
| - uint64_t id = start_id; |
| - do { |
| - std::ostringstream oss; |
| - oss << test::OutputPath() << "ivf_test_file_" << id++ << ".ivf"; |
| - file_name_ = oss.str(); |
| - } while ((id - start_id) < 100u && FileExists(false)); |
| - ASSERT_LT(id - start_id, 100u); |
| + file_name_ = |
| + webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); |
| } |
| + void TearDown() override { RemoveFile(file_name_); } |
| - bool WriteDummyTestFrames(int width, |
| + bool WriteDummyTestFrames(VideoCodecType codec_type, |
| + int width, |
| int height, |
| int num_frames, |
| bool use_capture_tims_ms) { |
| @@ -57,21 +66,22 @@ class IvfFileWriterTest : public ::testing::Test { |
| } else { |
| frame._timeStamp = i; |
| } |
| - if (!file_writer_->WriteFrame(frame)) |
| + if (!file_writer_->WriteFrame(frame, codec_type)) |
| return false; |
| } |
| return true; |
| } |
| - void VerifyIvfHeader(FileWrapper* file, |
| + void VerifyIvfHeader(rtc::File* file, |
| const uint8_t fourcc[4], |
| int width, |
| int height, |
| uint32_t num_frames, |
| bool use_capture_tims_ms) { |
| - ASSERT_TRUE(file->is_open()); |
| + ASSERT_TRUE(file->IsOpen()); |
| uint8_t data[kHeaderSize]; |
| - ASSERT_EQ(kHeaderSize, file->Read(data, kHeaderSize)); |
| + ASSERT_EQ(static_cast<unsigned int>(kHeaderSize), |
|
sprang_webrtc
2016/09/04 14:48:47
prefer uint32_t to unsigned int
|
| + file->Read(data, kHeaderSize)); |
| uint8_t dkif[4] = {'D', 'K', 'I', 'F'}; |
| EXPECT_EQ(0, memcmp(dkif, data, 4)); |
| @@ -87,11 +97,12 @@ class IvfFileWriterTest : public ::testing::Test { |
| EXPECT_EQ(0u, ByteReader<uint32_t>::ReadLittleEndian(&data[28])); |
| } |
| - void VerifyDummyTestFrames(FileWrapper* file, uint32_t num_frames) { |
| + void VerifyDummyTestFrames(rtc::File* file, uint32_t num_frames) { |
| const int kMaxFrameSize = 4; |
| for (uint32_t i = 1; i <= num_frames; ++i) { |
| uint8_t frame_header[kFrameHeaderSize]; |
| - ASSERT_EQ(kFrameHeaderSize, file->Read(frame_header, kFrameHeaderSize)); |
| + ASSERT_EQ(static_cast<unsigned int>(kFrameHeaderSize), |
| + file->Read(frame_header, kFrameHeaderSize)); |
| uint32_t frame_length = |
| ByteReader<uint32_t>::ReadLittleEndian(&frame_header[0]); |
| EXPECT_EQ(i % 4, frame_length); |
| @@ -109,67 +120,27 @@ class IvfFileWriterTest : public ::testing::Test { |
| void RunBasicFileStructureTest(VideoCodecType codec_type, |
| const uint8_t fourcc[4], |
| bool use_capture_tims_ms) { |
| - file_writer_ = IvfFileWriter::Open(file_name_, codec_type); |
| + file_writer_ = IvfFileWriter::Wrap(rtc::File::Open(file_name_)); |
| ASSERT_TRUE(file_writer_.get()); |
| const int kWidth = 320; |
| const int kHeight = 240; |
| const int kNumFrames = 257; |
| - EXPECT_TRUE( |
| - WriteDummyTestFrames(kWidth, kHeight, kNumFrames, use_capture_tims_ms)); |
| + ASSERT_TRUE(WriteDummyTestFrames(codec_type, kWidth, kHeight, kNumFrames, |
| + use_capture_tims_ms)); |
| EXPECT_TRUE(file_writer_->Close()); |
| - std::unique_ptr<FileWrapper> out_file(FileWrapper::Create()); |
| - ASSERT_TRUE(out_file->OpenFile(file_name_.c_str(), true)); |
| - VerifyIvfHeader(out_file.get(), fourcc, kWidth, kHeight, kNumFrames, |
| + rtc::File out_file = rtc::File::Open(file_name_); |
| + VerifyIvfHeader(&out_file, fourcc, kWidth, kHeight, kNumFrames, |
| use_capture_tims_ms); |
| - VerifyDummyTestFrames(out_file.get(), kNumFrames); |
| - |
| - out_file->CloseFile(); |
| - |
| - bool file_removed = false; |
| - for (int i = 0; i < kMaxFileRetries; ++i) { |
| - file_removed = remove(file_name_.c_str()) == 0; |
| - if (file_removed) |
| - break; |
| + VerifyDummyTestFrames(&out_file, kNumFrames); |
| - // Couldn't remove file for some reason, wait a sec and try again. |
| - rtc::Thread::SleepMs(1000); |
| - } |
| - EXPECT_TRUE(file_removed); |
| - } |
| - |
| - // Check whether file exists or not, and if it does not meet expectation, |
| - // wait a bit and check again, up to kMaxFileRetries times. This is an ugly |
| - // hack to avoid flakiness on certain operating systems where antivirus |
| - // software may unexpectedly lock files and keep them from disappearing or |
| - // being reused. |
| - bool FileExists(bool expected) { |
| - bool file_exists = expected; |
| - std::unique_ptr<FileWrapper> file_wrapper; |
| - int iterations = 0; |
| - do { |
| - if (file_wrapper.get() != nullptr) |
| - rtc::Thread::SleepMs(1000); |
| - file_wrapper.reset(FileWrapper::Create()); |
| - file_exists = file_wrapper->OpenFile(file_name_.c_str(), true); |
| - file_wrapper->CloseFile(); |
| - } while (file_exists != expected && ++iterations < kMaxFileRetries); |
| - return file_exists; |
| + out_file.Close(); |
| } |
| std::string file_name_; |
| std::unique_ptr<IvfFileWriter> file_writer_; |
| }; |
| -TEST_F(IvfFileWriterTest, RemovesUnusedFile) { |
| - file_writer_ = IvfFileWriter::Open(file_name_, kVideoCodecVP8); |
| - ASSERT_TRUE(file_writer_.get() != nullptr); |
| - EXPECT_TRUE(FileExists(true)); |
| - EXPECT_TRUE(file_writer_->Close()); |
| - EXPECT_FALSE(FileExists(false)); |
| - EXPECT_FALSE(file_writer_->Close()); // Can't close twice. |
| -} |
| - |
| TEST_F(IvfFileWriterTest, WritesBasicVP8FileNtpTimestamp) { |
| const uint8_t fourcc[4] = {'V', 'P', '8', '0'}; |
| RunBasicFileStructureTest(kVideoCodecVP8, fourcc, false); |