Delete rtc::File, usage replaced with FileWrapper

Bug: webrtc:6463
Change-Id: Ia0767a2e6bbacc43e63c30ed3bd3edb10ff6e645
Reviewed-on: https://webrtc-review.googlesource.com/c/121943
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26613}
This commit is contained in:
Niels Möller 2019-02-08 16:40:53 +01:00 committed by Commit Bot
parent 9f3aabb5ad
commit b7edf69e9a
20 changed files with 50 additions and 569 deletions

View File

@ -294,6 +294,7 @@ rtc_source_set("video_coding_utility") {
"../../rtc_base/experiments:quality_scaling_experiment", "../../rtc_base/experiments:quality_scaling_experiment",
"../../rtc_base/experiments:rate_control_settings", "../../rtc_base/experiments:rate_control_settings",
"../../rtc_base/system:arch", "../../rtc_base/system:arch",
"../../rtc_base/system:file_wrapper",
"../../rtc_base/task_utils:repeating_task", "../../rtc_base/task_utils:repeating_task",
"../../system_wrappers", "../../system_wrappers",
"../../system_wrappers:field_trial", "../../system_wrappers:field_trial",

View File

@ -39,7 +39,6 @@
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/cpu_time.h" #include "rtc_base/cpu_time.h"
#include "rtc_base/event.h" #include "rtc_base/event.h"
#include "rtc_base/file.h"
#include "rtc_base/strings/string_builder.h" #include "rtc_base/strings/string_builder.h"
#include "rtc_base/time_utils.h" #include "rtc_base/time_utils.h"
#include "system_wrappers/include/cpu_info.h" #include "system_wrappers/include/cpu_info.h"
@ -617,8 +616,8 @@ void VideoCodecTestFixtureImpl::SetUpAndInitObjects(
std::to_string(simulcast_svc_idx); std::to_string(simulcast_svc_idx);
if (config_.visualization_params.save_encoded_ivf) { if (config_.visualization_params.save_encoded_ivf) {
rtc::File post_encode_file = FileWrapper post_encode_file =
rtc::File::Create(output_filename_base + ".ivf"); FileWrapper::OpenWriteOnly(output_filename_base + ".ivf");
encoded_frame_writers_.push_back( encoded_frame_writers_.push_back(
IvfFileWriter::Wrap(std::move(post_encode_file), 0)); IvfFileWriter::Wrap(std::move(post_encode_file), 0));
} }

View File

@ -24,7 +24,7 @@ namespace webrtc {
const size_t kIvfHeaderSize = 32; const size_t kIvfHeaderSize = 32;
IvfFileWriter::IvfFileWriter(rtc::File file, size_t byte_limit) IvfFileWriter::IvfFileWriter(FileWrapper file, size_t byte_limit)
: codec_type_(kVideoCodecGeneric), : codec_type_(kVideoCodecGeneric),
bytes_written_(0), bytes_written_(0),
byte_limit_(byte_limit), byte_limit_(byte_limit),
@ -42,14 +42,14 @@ IvfFileWriter::~IvfFileWriter() {
Close(); Close();
} }
std::unique_ptr<IvfFileWriter> IvfFileWriter::Wrap(rtc::File file, std::unique_ptr<IvfFileWriter> IvfFileWriter::Wrap(FileWrapper file,
size_t byte_limit) { size_t byte_limit) {
return std::unique_ptr<IvfFileWriter>( return std::unique_ptr<IvfFileWriter>(
new IvfFileWriter(std::move(file), byte_limit)); new IvfFileWriter(std::move(file), byte_limit));
} }
bool IvfFileWriter::WriteHeader() { bool IvfFileWriter::WriteHeader() {
if (!file_.Seek(0)) { if (!file_.Rewind()) {
RTC_LOG(LS_WARNING) << "Unable to rewind ivf output file."; RTC_LOG(LS_WARNING) << "Unable to rewind ivf output file.";
return false; return false;
} }
@ -97,7 +97,7 @@ bool IvfFileWriter::WriteHeader() {
static_cast<uint32_t>(num_frames_)); static_cast<uint32_t>(num_frames_));
ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[28], 0); // Reserved. ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[28], 0); // Reserved.
if (file_.Write(ivf_header, kIvfHeaderSize) < kIvfHeaderSize) { if (!file_.Write(ivf_header, kIvfHeaderSize)) {
RTC_LOG(LS_ERROR) << "Unable to write IVF header for ivf output file."; RTC_LOG(LS_ERROR) << "Unable to write IVF header for ivf output file.";
return false; return false;
} }
@ -133,7 +133,7 @@ bool IvfFileWriter::InitFromFirstFrame(const EncodedImage& encoded_image,
bool IvfFileWriter::WriteFrame(const EncodedImage& encoded_image, bool IvfFileWriter::WriteFrame(const EncodedImage& encoded_image,
VideoCodecType codec_type) { VideoCodecType codec_type) {
if (!file_.IsOpen()) if (!file_.is_open())
return false; return false;
if (num_frames_ == 0 && !InitFromFirstFrame(encoded_image, codec_type)) if (num_frames_ == 0 && !InitFromFirstFrame(encoded_image, codec_type))
@ -170,9 +170,8 @@ bool IvfFileWriter::WriteFrame(const EncodedImage& encoded_image,
ByteWriter<uint32_t>::WriteLittleEndian( ByteWriter<uint32_t>::WriteLittleEndian(
&frame_header[0], static_cast<uint32_t>(encoded_image.size())); &frame_header[0], static_cast<uint32_t>(encoded_image.size()));
ByteWriter<uint64_t>::WriteLittleEndian(&frame_header[4], timestamp); ByteWriter<uint64_t>::WriteLittleEndian(&frame_header[4], timestamp);
if (file_.Write(frame_header, kFrameHeaderSize) < kFrameHeaderSize || if (!file_.Write(frame_header, kFrameHeaderSize) ||
file_.Write(encoded_image.data(), encoded_image.size()) < !file_.Write(encoded_image.data(), encoded_image.size())) {
encoded_image.size()) {
RTC_LOG(LS_ERROR) << "Unable to write frame to file."; RTC_LOG(LS_ERROR) << "Unable to write frame to file.";
return false; return false;
} }
@ -184,7 +183,7 @@ bool IvfFileWriter::WriteFrame(const EncodedImage& encoded_image,
} }
bool IvfFileWriter::Close() { bool IvfFileWriter::Close() {
if (!file_.IsOpen()) if (!file_.is_open())
return false; return false;
if (num_frames_ == 0) { if (num_frames_ == 0) {

View File

@ -18,7 +18,7 @@
#include "api/video/encoded_image.h" #include "api/video/encoded_image.h"
#include "common_types.h" // NOLINT(build/include) #include "common_types.h" // NOLINT(build/include)
#include "rtc_base/constructor_magic.h" #include "rtc_base/constructor_magic.h"
#include "rtc_base/file.h" #include "rtc_base/system/file_wrapper.h"
#include "rtc_base/time_utils.h" #include "rtc_base/time_utils.h"
namespace webrtc { namespace webrtc {
@ -29,14 +29,15 @@ class IvfFileWriter {
// Close or ~IvfFileWriter. If writing a frame would take the file above the // Close or ~IvfFileWriter. If writing a frame would take the file above the
// |byte_limit| the file will be closed, the write (and all future writes) // |byte_limit| the file will be closed, the write (and all future writes)
// will fail. A |byte_limit| of 0 is equivalent to no limit. // will fail. A |byte_limit| of 0 is equivalent to no limit.
static std::unique_ptr<IvfFileWriter> Wrap(rtc::File file, size_t byte_limit); static std::unique_ptr<IvfFileWriter> Wrap(FileWrapper file,
size_t byte_limit);
~IvfFileWriter(); ~IvfFileWriter();
bool WriteFrame(const EncodedImage& encoded_image, VideoCodecType codec_type); bool WriteFrame(const EncodedImage& encoded_image, VideoCodecType codec_type);
bool Close(); bool Close();
private: private:
explicit IvfFileWriter(rtc::File file, size_t byte_limit); explicit IvfFileWriter(FileWrapper file, size_t byte_limit);
bool WriteHeader(); bool WriteHeader();
bool InitFromFirstFrame(const EncodedImage& encoded_image, bool InitFromFirstFrame(const EncodedImage& encoded_image,
@ -51,7 +52,7 @@ class IvfFileWriter {
int64_t last_timestamp_; int64_t last_timestamp_;
bool using_capture_timestamps_; bool using_capture_timestamps_;
rtc::TimestampWrapAroundHandler wrap_handler_; rtc::TimestampWrapAroundHandler wrap_handler_;
rtc::File file_; FileWrapper file_;
RTC_DISALLOW_COPY_AND_ASSIGN(IvfFileWriter); RTC_DISALLOW_COPY_AND_ASSIGN(IvfFileWriter);
}; };

View File

@ -57,13 +57,13 @@ class IvfFileWriterTest : public ::testing::Test {
return true; return true;
} }
void VerifyIvfHeader(rtc::File* file, void VerifyIvfHeader(FileWrapper* file,
const uint8_t fourcc[4], const uint8_t fourcc[4],
int width, int width,
int height, int height,
uint32_t num_frames, uint32_t num_frames,
bool use_capture_tims_ms) { bool use_capture_tims_ms) {
ASSERT_TRUE(file->IsOpen()); ASSERT_TRUE(file->is_open());
uint8_t data[kHeaderSize]; uint8_t data[kHeaderSize];
ASSERT_EQ(static_cast<size_t>(kHeaderSize), file->Read(data, kHeaderSize)); ASSERT_EQ(static_cast<size_t>(kHeaderSize), file->Read(data, kHeaderSize));
@ -81,7 +81,7 @@ class IvfFileWriterTest : public ::testing::Test {
EXPECT_EQ(0u, ByteReader<uint32_t>::ReadLittleEndian(&data[28])); EXPECT_EQ(0u, ByteReader<uint32_t>::ReadLittleEndian(&data[28]));
} }
void VerifyDummyTestFrames(rtc::File* file, uint32_t num_frames) { void VerifyDummyTestFrames(FileWrapper* file, uint32_t num_frames) {
const int kMaxFrameSize = 4; const int kMaxFrameSize = 4;
for (uint32_t i = 1; i <= num_frames; ++i) { for (uint32_t i = 1; i <= num_frames; ++i) {
uint8_t frame_header[kFrameHeaderSize]; uint8_t frame_header[kFrameHeaderSize];
@ -104,7 +104,8 @@ class IvfFileWriterTest : public ::testing::Test {
void RunBasicFileStructureTest(VideoCodecType codec_type, void RunBasicFileStructureTest(VideoCodecType codec_type,
const uint8_t fourcc[4], const uint8_t fourcc[4],
bool use_capture_tims_ms) { bool use_capture_tims_ms) {
file_writer_ = IvfFileWriter::Wrap(rtc::File::Open(file_name_), 0); file_writer_ =
IvfFileWriter::Wrap(FileWrapper::OpenWriteOnly(file_name_), 0);
ASSERT_TRUE(file_writer_.get()); ASSERT_TRUE(file_writer_.get());
const int kWidth = 320; const int kWidth = 320;
const int kHeight = 240; const int kHeight = 240;
@ -113,7 +114,7 @@ class IvfFileWriterTest : public ::testing::Test {
use_capture_tims_ms)); use_capture_tims_ms));
EXPECT_TRUE(file_writer_->Close()); EXPECT_TRUE(file_writer_->Close());
rtc::File out_file = rtc::File::Open(file_name_); FileWrapper out_file = FileWrapper::OpenReadOnly(file_name_);
VerifyIvfHeader(&out_file, fourcc, kWidth, kHeight, kNumFrames, VerifyIvfHeader(&out_file, fourcc, kWidth, kHeight, kNumFrames,
use_capture_tims_ms); use_capture_tims_ms);
VerifyDummyTestFrames(&out_file, kNumFrames); VerifyDummyTestFrames(&out_file, kNumFrames);
@ -163,7 +164,7 @@ TEST_F(IvfFileWriterTest, ClosesWhenReachesLimit) {
const int kNumFramesToFit = 1; const int kNumFramesToFit = 1;
file_writer_ = IvfFileWriter::Wrap( file_writer_ = IvfFileWriter::Wrap(
rtc::File::Open(file_name_), FileWrapper::OpenWriteOnly(file_name_),
kHeaderSize + kHeaderSize +
kNumFramesToFit * (kFrameHeaderSize + sizeof(dummy_payload))); kNumFramesToFit * (kFrameHeaderSize + sizeof(dummy_payload)));
ASSERT_TRUE(file_writer_.get()); ASSERT_TRUE(file_writer_.get());
@ -172,7 +173,7 @@ TEST_F(IvfFileWriterTest, ClosesWhenReachesLimit) {
kNumFramesToWrite, true)); kNumFramesToWrite, true));
ASSERT_FALSE(file_writer_->Close()); ASSERT_FALSE(file_writer_->Close());
rtc::File out_file = rtc::File::Open(file_name_); FileWrapper out_file = FileWrapper::OpenReadOnly(file_name_);
VerifyIvfHeader(&out_file, fourcc, kWidth, kHeight, kNumFramesToFit, true); VerifyIvfHeader(&out_file, fourcc, kWidth, kHeight, kNumFramesToFit, true);
VerifyDummyTestFrames(&out_file, kNumFramesToFit); VerifyDummyTestFrames(&out_file, kNumFramesToFit);

View File

@ -105,8 +105,6 @@ rtc_source_set("rtc_base_approved") {
"copy_on_write_buffer.h", "copy_on_write_buffer.h",
"event_tracer.cc", "event_tracer.cc",
"event_tracer.h", "event_tracer.h",
"file.cc",
"file.h",
"flags.cc", "flags.cc",
"flags.h", "flags.h",
"function_view.h", "function_view.h",
@ -140,13 +138,8 @@ rtc_source_set("rtc_base_approved") {
"zero_memory.h", "zero_memory.h",
] ]
if (is_posix || is_fuchsia) {
sources += [ "file_posix.cc" ]
}
if (is_win) { if (is_win) {
sources += [ sources += [
"file_win.cc",
"win/windows_version.cc", "win/windows_version.cc",
"win/windows_version.h", "win/windows_version.h",
] ]
@ -1253,7 +1246,6 @@ if (rtc_include_tests) {
"critical_section_unittest.cc", "critical_section_unittest.cc",
"event_tracer_unittest.cc", "event_tracer_unittest.cc",
"event_unittest.cc", "event_unittest.cc",
"file_unittest.cc",
"function_view_unittest.cc", "function_view_unittest.cc",
"logging_unittest.cc", "logging_unittest.cc",
"numerics/histogram_percentile_counter_unittest.cc", "numerics/histogram_percentile_counter_unittest.cc",

View File

@ -1,53 +0,0 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/file.h"
namespace rtc {
File::File(PlatformFile file) : file_(file) {}
File::File() : file_(kInvalidPlatformFileValue) {}
File::~File() {
Close();
}
// static
File File::Open(const std::string& path) {
return File(OpenPlatformFile(path));
}
// static
File File::Create(const std::string& path) {
return File(CreatePlatformFile(path));
}
// static
bool File::Remove(const std::string& path) {
return RemoveFile(path);
}
File::File(File&& other) : file_(other.file_) {
other.file_ = kInvalidPlatformFileValue;
}
File& File::operator=(File&& other) {
Close();
file_ = other.file_;
other.file_ = kInvalidPlatformFileValue;
return *this;
}
bool File::IsOpen() {
return file_ != kInvalidPlatformFileValue;
}
} // namespace rtc

View File

@ -1,75 +0,0 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_FILE_H_
#define RTC_BASE_FILE_H_
#include <stddef.h>
#include <stdint.h>
#include <string>
#include "rtc_base/constructor_magic.h"
#include "rtc_base/platform_file.h"
namespace rtc {
// This class wraps the platform specific APIs for simple file interactions.
//
// The various read and write methods are best effort, i.e. if an underlying
// call does not manage to read/write all the data more calls will be performed,
// until an error is detected or all data is read/written.
class File {
public:
// Wraps the given PlatformFile. This class is then responsible for closing
// the file, which will be done in the destructor if Close is never called.
explicit File(PlatformFile);
// The default constructor produces a closed file.
File();
~File();
File(File&& other);
File& operator=(File&& other);
// Open and Create give files with both reading and writing enabled.
static File Open(const std::string& path);
// If the file already exists it will be overwritten.
static File Create(const std::string& path);
// Remove a file in the file system.
static bool Remove(const std::string& path);
size_t Write(const uint8_t* data, size_t length);
size_t Read(uint8_t* buffer, size_t length);
// The current position in the file after a call to these methods is platform
// dependent (MSVC gives position offset+length, most other
// compilers/platforms do not alter the position), i.e. do not depend on it,
// do a Seek before any subsequent Read/Write.
size_t WriteAt(const uint8_t* data, size_t length, size_t offset);
size_t ReadAt(uint8_t* buffer, size_t length, size_t offset);
// Attempt to position the file at the given offset from the start.
// Returns true if successful, false otherwise.
bool Seek(size_t offset);
// Attempt to close the file. Returns true if successful, false otherwise,
// most notably when the file is already closed.
bool Close();
bool IsOpen();
private:
PlatformFile file_;
RTC_DISALLOW_COPY_AND_ASSIGN(File);
};
} // namespace rtc
#endif // RTC_BASE_FILE_H_

View File

@ -1,95 +0,0 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <limits>
#include "rtc_base/checks.h"
#include "rtc_base/file.h"
#include "rtc_base/platform_file.h"
namespace rtc {
size_t File::Write(const uint8_t* data, size_t length) {
size_t total_written = 0;
do {
ssize_t written;
do {
written = ::write(file_, data + total_written, length - total_written);
} while (written == -1 && errno == EINTR);
if (written == -1)
break;
total_written += written;
} while (total_written < length);
return total_written;
}
size_t File::Read(uint8_t* buffer, size_t length) {
size_t total_read = 0;
do {
ssize_t read;
do {
read = ::read(file_, buffer + total_read, length - total_read);
} while (read == -1 && errno == EINTR);
if (read == -1)
break;
total_read += read;
} while (total_read < length);
return total_read;
}
size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) {
size_t total_written = 0;
do {
ssize_t written;
do {
written = ::pwrite(file_, data + total_written, length - total_written,
offset + total_written);
} while (written == -1 && errno == EINTR);
if (written == -1)
break;
total_written += written;
} while (total_written < length);
return total_written;
}
size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) {
size_t total_read = 0;
do {
ssize_t read;
do {
read = ::pread(file_, buffer + total_read, length - total_read,
offset + total_read);
} while (read == -1 && errno == EINTR);
if (read == -1)
break;
total_read += read;
} while (total_read < length);
return total_read;
}
bool File::Seek(size_t offset) {
RTC_DCHECK_LE(offset, std::numeric_limits<off_t>::max());
return lseek(file_, static_cast<off_t>(offset), SEEK_SET) != -1;
}
bool File::Close() {
if (file_ == rtc::kInvalidPlatformFileValue)
return false;
bool ret = close(file_) == 0;
file_ = rtc::kInvalidPlatformFileValue;
return ret;
}
} // namespace rtc

View File

@ -1,174 +0,0 @@
/*
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <string.h>
#include <string>
#include "rtc_base/file.h"
#include "test/gtest.h"
#include "test/testsupport/file_utils.h"
#if defined(WEBRTC_WIN)
#include "rtc_base/win32.h"
#else // if defined(WEBRTC_WIN)
#include <errno.h>
#endif
namespace rtc {
int LastError() {
#if defined(WEBRTC_WIN)
return ::GetLastError();
#else
return errno;
#endif
}
bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) {
for (size_t i = 0; i < length; ++i) {
uint8_t val = start_value++;
EXPECT_EQ(val, buffer[i]);
if (buffer[i] != val)
return false;
}
// Prevent the same buffer from being verified multiple times simply
// because some operation that should have written to it failed
memset(buffer, 0, length);
return true;
}
class FileTest : public ::testing::Test {
protected:
std::string path_;
void SetUp() override {
path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file");
ASSERT_FALSE(path_.empty());
}
void TearDown() override { RemoveFile(path_); }
};
TEST_F(FileTest, DefaultConstructor) {
File file;
uint8_t buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
EXPECT_FALSE(file.IsOpen());
EXPECT_EQ(0u, file.Write(buffer, 10));
EXPECT_FALSE(file.Seek(0));
EXPECT_EQ(0u, file.Read(buffer, 10));
EXPECT_EQ(0u, file.WriteAt(buffer, 10, 0));
EXPECT_EQ(0u, file.ReadAt(buffer, 10, 0));
EXPECT_FALSE(file.Close());
}
TEST_F(FileTest, DoubleClose) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
EXPECT_TRUE(file.Close());
EXPECT_FALSE(file.Close());
}
TEST_F(FileTest, SimpleReadWrite) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[100] = {0};
uint8_t out[100] = {0};
for (int i = 0; i < 100; ++i) {
data[i] = i;
}
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_TRUE(file.Seek(0));
EXPECT_EQ(10u, file.Read(out, 10));
EXPECT_TRUE(VerifyBuffer(out, 10, 0));
EXPECT_TRUE(file.Seek(0));
EXPECT_EQ(100u, file.Write(data, 100));
EXPECT_TRUE(file.Seek(0));
EXPECT_EQ(100u, file.Read(out, 100));
EXPECT_TRUE(VerifyBuffer(out, 100, 0));
EXPECT_TRUE(file.Seek(1));
EXPECT_EQ(50u, file.Write(data, 50));
EXPECT_EQ(50u, file.Write(data + 50, 50));
EXPECT_TRUE(file.Seek(1));
EXPECT_EQ(100u, file.Read(out, 100));
EXPECT_TRUE(VerifyBuffer(out, 100, 0));
}
TEST_F(FileTest, ReadWriteClose) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
uint8_t out[10] = {0};
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_TRUE(file.Close());
File file2 = File::Open(path_);
ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError();
EXPECT_EQ(10u, file2.Read(out, 10));
EXPECT_TRUE(VerifyBuffer(out, 10, 0));
}
TEST_F(FileTest, RandomAccessRead) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
uint8_t out[10] = {0};
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_EQ(4u, file.ReadAt(out, 4, 0));
EXPECT_TRUE(VerifyBuffer(out, 4, 0));
EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
EXPECT_TRUE(VerifyBuffer(out, 4, 4));
EXPECT_EQ(5u, file.ReadAt(out, 5, 5));
EXPECT_TRUE(VerifyBuffer(out, 5, 5));
}
TEST_F(FileTest, RandomAccessReadWrite) {
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
uint8_t out[10] = {0};
EXPECT_EQ(10u, file.Write(data, 10));
EXPECT_TRUE(file.Seek(4));
EXPECT_EQ(4u, file.WriteAt(data, 4, 4));
EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
EXPECT_TRUE(VerifyBuffer(out, 4, 0));
EXPECT_EQ(2u, file.WriteAt(data, 2, 8));
EXPECT_EQ(2u, file.ReadAt(out, 2, 8));
EXPECT_TRUE(VerifyBuffer(out, 2, 0));
}
TEST_F(FileTest, ShouldBeAbleToRemoveFile) {
{
File file = File::Open(path_);
ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
}
ASSERT_TRUE(File::Remove(path_)) << "Error: " << LastError();
}
} // namespace rtc

View File

@ -1,113 +0,0 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/file.h"
#include <io.h>
#include <windows.h>
#include <limits>
#include "rtc_base/checks.h"
namespace rtc {
size_t File::Write(const uint8_t* data, size_t length) {
RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
size_t total_written = 0;
do {
DWORD written;
if (!::WriteFile(file_, data + total_written,
static_cast<DWORD>(length - total_written), &written,
nullptr)) {
break;
}
total_written += written;
} while (total_written < length);
return total_written;
}
size_t File::Read(uint8_t* buffer, size_t length) {
RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
size_t total_read = 0;
do {
DWORD read;
if (!::ReadFile(file_, buffer + total_read,
static_cast<DWORD>(length - total_read), &read, nullptr)) {
break;
}
total_read += read;
} while (total_read < length);
return total_read;
}
size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) {
RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
size_t total_written = 0;
do {
DWORD written;
LARGE_INTEGER offset_li;
offset_li.QuadPart = offset + total_written;
OVERLAPPED overlapped = {0};
overlapped.Offset = offset_li.LowPart;
overlapped.OffsetHigh = offset_li.HighPart;
if (!::WriteFile(file_, data + total_written,
static_cast<DWORD>(length - total_written), &written,
&overlapped)) {
break;
}
total_written += written;
} while (total_written < length);
return total_written;
}
size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) {
RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max());
size_t total_read = 0;
do {
DWORD read;
LARGE_INTEGER offset_li;
offset_li.QuadPart = offset + total_read;
OVERLAPPED overlapped = {0};
overlapped.Offset = offset_li.LowPart;
overlapped.OffsetHigh = offset_li.HighPart;
if (!::ReadFile(file_, buffer + total_read,
static_cast<DWORD>(length - total_read), &read,
&overlapped)) {
break;
}
total_read += read;
} while (total_read < length);
return total_read;
}
bool File::Seek(size_t offset) {
LARGE_INTEGER distance;
distance.QuadPart = offset;
return SetFilePointerEx(file_, distance, nullptr, FILE_BEGIN) != 0;
}
bool File::Close() {
if (file_ == kInvalidPlatformFileValue)
return false;
bool ret = CloseHandle(file_) != 0;
file_ = kInvalidPlatformFileValue;
return ret;
}
} // namespace rtc

View File

@ -71,7 +71,6 @@ class FileWrapper final {
// Seeks to the beginning of file. Returns true on success, false on failure, // Seeks to the beginning of file. Returns true on success, false on failure,
// e.g., if the underlying file isn't seekable. // e.g., if the underlying file isn't seekable.
// TODO(nisse): Delete this method.
bool Rewind(); bool Rewind();
// Returns number of bytes read. Short count indicates EOF or error. // Returns number of bytes read. Short count indicates EOF or error.

View File

@ -310,6 +310,7 @@ if (rtc_include_tests) {
deps = [ deps = [
":fileutils", ":fileutils",
"../rtc_base:rtc_base_approved", "../rtc_base:rtc_base_approved",
"../rtc_base/system:file_wrapper",
] ]
} }
@ -361,6 +362,7 @@ if (rtc_include_tests) {
"../modules/video_capture", "../modules/video_capture",
"../modules/video_coding:simulcast_test_fixture_impl", "../modules/video_coding:simulcast_test_fixture_impl",
"../rtc_base:rtc_base_approved", "../rtc_base:rtc_base_approved",
"../rtc_base/system:file_wrapper",
"../test:single_threaded_task_queue", "../test:single_threaded_task_queue",
"pc/e2e:e2e_unittests", "pc/e2e:e2e_unittests",
"scenario:scenario_unittests", "scenario:scenario_unittests",

View File

@ -12,9 +12,9 @@
#include <string.h> #include <string.h>
#include "rtc_base/file.h"
#include "rtc_base/flags.h" #include "rtc_base/flags.h"
#include "rtc_base/logging.h" #include "rtc_base/logging.h"
#include "rtc_base/system/file_wrapper.h"
#include "test/testsupport/file_utils.h" #include "test/testsupport/file_utils.h"
namespace { namespace {
@ -53,10 +53,10 @@ bool WriteToTestArtifactsDir(const char* filename,
return false; return false;
} }
rtc::File output = FileWrapper output = FileWrapper::OpenWriteOnly(
rtc::File::Create(JoinFilename(FLAG_test_artifacts_dir, filename)); JoinFilename(FLAG_test_artifacts_dir, filename));
return output.IsOpen() && output.Write(buffer, length) == length; return output.is_open() && output.Write(buffer, length);
} }
bool WriteToTestArtifactsDir(const char* filename, const std::string& content) { bool WriteToTestArtifactsDir(const char* filename, const std::string& content) {

View File

@ -13,8 +13,8 @@
#include <string.h> #include <string.h>
#include <string> #include <string>
#include "rtc_base/file.h"
#include "rtc_base/flags.h" #include "rtc_base/flags.h"
#include "rtc_base/system/file_wrapper.h"
#include "test/gtest.h" #include "test/gtest.h"
#include "test/testsupport/file_utils.h" #include "test/testsupport/file_utils.h"
@ -41,9 +41,9 @@ TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) {
const char* content = "some-contents"; const char* content = "some-contents";
if (WriteToTestArtifactsDir(filename, content)) { if (WriteToTestArtifactsDir(filename, content)) {
std::string out_file = JoinFilename(FLAG_test_artifacts_dir, filename); std::string out_file = JoinFilename(FLAG_test_artifacts_dir, filename);
rtc::File input = rtc::File::Open(out_file); FileWrapper input = FileWrapper::OpenReadOnly(out_file);
EXPECT_TRUE(input.IsOpen()); EXPECT_TRUE(input.is_open());
EXPECT_TRUE(input.Seek(0)); EXPECT_TRUE(input.Rewind());
uint8_t buffer[32]; uint8_t buffer[32];
EXPECT_EQ(input.Read(buffer, strlen(content)), strlen(content)); EXPECT_EQ(input.Read(buffer, strlen(content)), strlen(content));
buffer[strlen(content)] = 0; buffer[strlen(content)] = 0;
@ -51,7 +51,7 @@ TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) {
std::string(reinterpret_cast<char*>(buffer))); std::string(reinterpret_cast<char*>(buffer)));
input.Close(); input.Close();
EXPECT_TRUE(rtc::File::Remove(out_file)); EXPECT_TRUE(RemoveFile(out_file));
} }
} }

View File

@ -13,14 +13,13 @@
#include <utility> #include <utility>
#include "modules/video_coding/include/video_codec_interface.h" #include "modules/video_coding/include/video_codec_interface.h"
#include "rtc_base/file.h"
namespace webrtc { namespace webrtc {
FrameDumpingDecoder::FrameDumpingDecoder(std::unique_ptr<VideoDecoder> decoder, FrameDumpingDecoder::FrameDumpingDecoder(std::unique_ptr<VideoDecoder> decoder,
rtc::PlatformFile file) FileWrapper file)
: decoder_(std::move(decoder)), : decoder_(std::move(decoder)),
writer_(IvfFileWriter::Wrap(rtc::File(file), writer_(IvfFileWriter::Wrap(std::move(file),
/* byte_limit= */ 100000000)) {} /* byte_limit= */ 100000000)) {}
FrameDumpingDecoder::~FrameDumpingDecoder() = default; FrameDumpingDecoder::~FrameDumpingDecoder() = default;

View File

@ -19,15 +19,13 @@
#include "api/video_codecs/video_decoder.h" #include "api/video_codecs/video_decoder.h"
#include "modules/video_coding/include/video_codec_interface.h" #include "modules/video_coding/include/video_codec_interface.h"
#include "modules/video_coding/utility/ivf_file_writer.h" #include "modules/video_coding/utility/ivf_file_writer.h"
#include "rtc_base/platform_file.h"
namespace webrtc { namespace webrtc {
// A decoder wrapper that writes the encoded frames to a file. // A decoder wrapper that writes the encoded frames to a file.
class FrameDumpingDecoder : public VideoDecoder { class FrameDumpingDecoder : public VideoDecoder {
public: public:
FrameDumpingDecoder(std::unique_ptr<VideoDecoder> decoder, FrameDumpingDecoder(std::unique_ptr<VideoDecoder> decoder, FileWrapper file);
rtc::PlatformFile file);
~FrameDumpingDecoder() override; ~FrameDumpingDecoder() override;
int32_t InitDecode(const VideoCodec* codec_settings, int32_t InitDecode(const VideoCodec* codec_settings,

View File

@ -87,11 +87,11 @@ class QualityTestVideoEncoder : public VideoEncoder,
public: public:
QualityTestVideoEncoder(std::unique_ptr<VideoEncoder> encoder, QualityTestVideoEncoder(std::unique_ptr<VideoEncoder> encoder,
VideoAnalyzer* analyzer, VideoAnalyzer* analyzer,
std::vector<rtc::PlatformFile> files) std::vector<FileWrapper> files)
: encoder_(std::move(encoder)), analyzer_(analyzer) { : encoder_(std::move(encoder)), analyzer_(analyzer) {
for (rtc::PlatformFile file : files) { for (FileWrapper& file : files) {
writers_.push_back( writers_.push_back(
IvfFileWriter::Wrap(rtc::File(file), /* byte_limit= */ 100000000)); IvfFileWriter::Wrap(std::move(file), /* byte_limit= */ 100000000));
} }
} }
// Implement VideoEncoder // Implement VideoEncoder
@ -182,7 +182,7 @@ std::unique_ptr<VideoDecoder> VideoQualityTest::CreateVideoDecoder(
std::string path = std::string path =
params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf"; params_.logging.encoded_frame_base_path + "." + str.str() + ".recv.ivf";
decoder = absl::make_unique<FrameDumpingDecoder>( decoder = absl::make_unique<FrameDumpingDecoder>(
std::move(decoder), rtc::CreatePlatformFile(path)); std::move(decoder), FileWrapper::OpenWriteOnly(path));
} }
return decoder; return decoder;
} }
@ -208,15 +208,16 @@ std::unique_ptr<VideoEncoder> VideoQualityTest::CreateVideoEncoder(
sb << send_logs_++; sb << send_logs_++;
std::string prefix = std::string prefix =
params_.logging.encoded_frame_base_path + "." + sb.str() + ".send."; params_.logging.encoded_frame_base_path + "." + sb.str() + ".send.";
std::vector<FileWrapper> files;
files.push_back(FileWrapper::OpenWriteOnly(prefix + "1.ivf"));
files.push_back(FileWrapper::OpenWriteOnly(prefix + "2.ivf"));
files.push_back(FileWrapper::OpenWriteOnly(prefix + "3.ivf"));
encoder = absl::make_unique<QualityTestVideoEncoder>( encoder = absl::make_unique<QualityTestVideoEncoder>(
std::move(encoder), analyzer, std::move(encoder), analyzer, std::move(files));
std::vector<rtc::PlatformFile>(
{rtc::CreatePlatformFile(prefix + "1.ivf"),
rtc::CreatePlatformFile(prefix + "2.ivf"),
rtc::CreatePlatformFile(prefix + "3.ivf")}));
} else if (analyzer) { } else if (analyzer) {
encoder = absl::make_unique<QualityTestVideoEncoder>( encoder = absl::make_unique<QualityTestVideoEncoder>(
std::move(encoder), analyzer, std::vector<rtc::PlatformFile>()); std::move(encoder), analyzer, std::vector<FileWrapper>());
} }
return encoder; return encoder;

View File

@ -319,7 +319,7 @@ void VideoReceiveStream::Start() {
ssb << decoded_output_file << "/webrtc_receive_stream_" ssb << decoded_output_file << "/webrtc_receive_stream_"
<< this->config_.rtp.local_ssrc << ".ivf"; << this->config_.rtp.local_ssrc << ".ivf";
video_decoder = absl::make_unique<FrameDumpingDecoder>( video_decoder = absl::make_unique<FrameDumpingDecoder>(
std::move(video_decoder), rtc::CreatePlatformFile(ssb.str())); std::move(video_decoder), FileWrapper::OpenWriteOnly(ssb.str()));
} }
video_decoders_.push_back(std::move(video_decoder)); video_decoders_.push_back(std::move(video_decoder));

View File

@ -23,7 +23,6 @@
#include "media/engine/internal_decoder_factory.h" #include "media/engine/internal_decoder_factory.h"
#include "modules/rtp_rtcp/include/rtp_header_parser.h" #include "modules/rtp_rtcp/include/rtp_header_parser.h"
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "rtc_base/file.h"
#include "rtc_base/flags.h" #include "rtc_base/flags.h"
#include "rtc_base/string_to_number.h" #include "rtc_base/string_to_number.h"
#include "rtc_base/strings/json.h" #include "rtc_base/strings/json.h"