| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" | 11 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 | 14 |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
| 17 #include "webrtc/base/thread.h" |
| 17 #include "webrtc/base/timeutils.h" | 18 #include "webrtc/base/timeutils.h" |
| 18 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 19 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 19 #include "webrtc/test/testsupport/fileutils.h" | 20 #include "webrtc/test/testsupport/fileutils.h" |
| 20 | 21 |
| 21 namespace webrtc { | 22 namespace webrtc { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 static const int kHeaderSize = 32; | 25 static const int kHeaderSize = 32; |
| 25 static const int kFrameHeaderSize = 12; | 26 static const int kFrameHeaderSize = 12; |
| 26 static uint8_t dummy_payload[4] = {0, 1, 2, 3}; | 27 static uint8_t dummy_payload[4] = {0, 1, 2, 3}; |
| 28 static const int kMaxFileRetries = 5; |
| 27 } // namespace | 29 } // namespace |
| 28 | 30 |
| 29 class IvfFileWriterTest : public ::testing::Test { | 31 class IvfFileWriterTest : public ::testing::Test { |
| 30 protected: | 32 protected: |
| 31 void SetUp() override { | 33 void SetUp() override { |
| 32 const int64_t start_id = | 34 const int64_t start_id = |
| 33 reinterpret_cast<int64_t>(this) ^ rtc::TimeMicros(); | 35 reinterpret_cast<int64_t>(this) ^ rtc::TimeMicros(); |
| 34 int64_t id = start_id; | 36 int64_t id = start_id; |
| 35 do { | 37 do { |
| 36 std::ostringstream oss; | 38 std::ostringstream oss; |
| 37 oss << test::OutputPath() << "ivf_test_file_" << id++ << ".ivf"; | 39 oss << test::OutputPath() << "ivf_test_file_" << id++ << ".ivf"; |
| 38 file_name_ = oss.str(); | 40 file_name_ = oss.str(); |
| 39 } while (id < start_id + 100 && FileExists()); | 41 } while (id < start_id + 100 && FileExists(false)); |
| 40 ASSERT_LT(id, start_id + 100); | 42 ASSERT_LT(id, start_id + 100); |
| 41 } | 43 } |
| 42 | 44 |
| 43 bool WriteDummyTestFrames(int width, | 45 bool WriteDummyTestFrames(int width, |
| 44 int height, | 46 int height, |
| 45 int num_frames, | 47 int num_frames, |
| 46 bool use_capture_tims_ms) { | 48 bool use_capture_tims_ms) { |
| 47 EncodedImage frame; | 49 EncodedImage frame; |
| 48 frame._buffer = dummy_payload; | 50 frame._buffer = dummy_payload; |
| 49 frame._encodedWidth = width; | 51 frame._encodedWidth = width; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 EXPECT_TRUE( | 116 EXPECT_TRUE( |
| 115 WriteDummyTestFrames(kWidth, kHeight, kNumFrames, use_capture_tims_ms)); | 117 WriteDummyTestFrames(kWidth, kHeight, kNumFrames, use_capture_tims_ms)); |
| 116 EXPECT_TRUE(file_writer_->Close()); | 118 EXPECT_TRUE(file_writer_->Close()); |
| 117 | 119 |
| 118 std::unique_ptr<FileWrapper> out_file(FileWrapper::Create()); | 120 std::unique_ptr<FileWrapper> out_file(FileWrapper::Create()); |
| 119 ASSERT_EQ(0, out_file->OpenFile(file_name_.c_str(), true)); | 121 ASSERT_EQ(0, out_file->OpenFile(file_name_.c_str(), true)); |
| 120 VerifyIvfHeader(out_file.get(), fourcc, kWidth, kHeight, kNumFrames, | 122 VerifyIvfHeader(out_file.get(), fourcc, kWidth, kHeight, kNumFrames, |
| 121 use_capture_tims_ms); | 123 use_capture_tims_ms); |
| 122 VerifyDummyTestFrames(out_file.get(), kNumFrames); | 124 VerifyDummyTestFrames(out_file.get(), kNumFrames); |
| 123 | 125 |
| 126 out_file->Flush(); |
| 124 EXPECT_EQ(0, out_file->CloseFile()); | 127 EXPECT_EQ(0, out_file->CloseFile()); |
| 125 EXPECT_EQ(0, remove(file_name_.c_str())); | 128 |
| 129 bool file_removed = false; |
| 130 for (int i = 0; i < kMaxFileRetries; ++i) { |
| 131 file_removed = remove(file_name_.c_str()) == 0; |
| 132 if (file_removed) |
| 133 break; |
| 134 |
| 135 // Couldn't remove file for some reason, wait a sec and try again. |
| 136 rtc::Thread::SleepMs(1000); |
| 137 } |
| 138 EXPECT_TRUE(file_removed); |
| 126 } | 139 } |
| 127 | 140 |
| 128 bool FileExists() { | 141 // Check whether file exists or not, and if it does not meet expectation, |
| 129 std::unique_ptr<FileWrapper> file_wrapper(FileWrapper::Create()); | 142 // wait a bit and check again, up to kMaxFileRetries times. This is an ugly |
| 130 return file_wrapper->OpenFile(file_name_.c_str(), true) == 0; | 143 // hack to avoid flakiness on certain operating systems where antivirus |
| 144 // software may unexpectedly lock files and keep them from disappearing or |
| 145 // being reused. |
| 146 bool FileExists(bool expected) { |
| 147 bool file_exists = expected; |
| 148 std::unique_ptr<FileWrapper> file_wrapper; |
| 149 int iterations = 0; |
| 150 do { |
| 151 if (file_wrapper.get() != nullptr) |
| 152 rtc::Thread::SleepMs(1000); |
| 153 file_wrapper.reset(FileWrapper::Create()); |
| 154 file_exists = file_wrapper->OpenFile(file_name_.c_str(), true) == 0; |
| 155 file_wrapper->CloseFile(); |
| 156 } while (file_exists != expected && ++iterations < kMaxFileRetries); |
| 157 return file_exists; |
| 131 } | 158 } |
| 132 | 159 |
| 133 std::string file_name_; | 160 std::string file_name_; |
| 134 std::unique_ptr<IvfFileWriter> file_writer_; | 161 std::unique_ptr<IvfFileWriter> file_writer_; |
| 135 }; | 162 }; |
| 136 | 163 |
| 137 TEST_F(IvfFileWriterTest, RemovesUnusedFile) { | 164 TEST_F(IvfFileWriterTest, RemovesUnusedFile) { |
| 138 file_writer_ = IvfFileWriter::Open(file_name_, kVideoCodecVP8); | 165 file_writer_ = IvfFileWriter::Open(file_name_, kVideoCodecVP8); |
| 139 ASSERT_TRUE(file_writer_.get() != nullptr); | 166 ASSERT_TRUE(file_writer_.get() != nullptr); |
| 140 EXPECT_TRUE(FileExists()); | 167 EXPECT_TRUE(FileExists(true)); |
| 141 EXPECT_TRUE(file_writer_->Close()); | 168 EXPECT_TRUE(file_writer_->Close()); |
| 142 EXPECT_FALSE(FileExists()); | 169 EXPECT_FALSE(FileExists(false)); |
| 143 EXPECT_FALSE(file_writer_->Close()); // Can't close twice. | 170 EXPECT_FALSE(file_writer_->Close()); // Can't close twice. |
| 144 } | 171 } |
| 145 | 172 |
| 146 TEST_F(IvfFileWriterTest, WritesBasicVP8FileNtpTimestamp) { | 173 TEST_F(IvfFileWriterTest, WritesBasicVP8FileNtpTimestamp) { |
| 147 const uint8_t fourcc[4] = {'V', 'P', '8', '0'}; | 174 const uint8_t fourcc[4] = {'V', 'P', '8', '0'}; |
| 148 RunBasicFileStructureTest(kVideoCodecVP8, fourcc, false); | 175 RunBasicFileStructureTest(kVideoCodecVP8, fourcc, false); |
| 149 } | 176 } |
| 150 | 177 |
| 151 TEST_F(IvfFileWriterTest, WritesBasicVP8FileMsTimestamp) { | 178 TEST_F(IvfFileWriterTest, WritesBasicVP8FileMsTimestamp) { |
| 152 const uint8_t fourcc[4] = {'V', 'P', '8', '0'}; | 179 const uint8_t fourcc[4] = {'V', 'P', '8', '0'}; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 167 const uint8_t fourcc[4] = {'H', '2', '6', '4'}; | 194 const uint8_t fourcc[4] = {'H', '2', '6', '4'}; |
| 168 RunBasicFileStructureTest(kVideoCodecH264, fourcc, false); | 195 RunBasicFileStructureTest(kVideoCodecH264, fourcc, false); |
| 169 } | 196 } |
| 170 | 197 |
| 171 TEST_F(IvfFileWriterTest, WritesBasicH264FileMsTimestamp) { | 198 TEST_F(IvfFileWriterTest, WritesBasicH264FileMsTimestamp) { |
| 172 const uint8_t fourcc[4] = {'H', '2', '6', '4'}; | 199 const uint8_t fourcc[4] = {'H', '2', '6', '4'}; |
| 173 RunBasicFileStructureTest(kVideoCodecH264, fourcc, true); | 200 RunBasicFileStructureTest(kVideoCodecH264, fourcc, true); |
| 174 } | 201 } |
| 175 | 202 |
| 176 } // namespace webrtc | 203 } // namespace webrtc |
| OLD | NEW |