add aec3 code.
This commit is contained in:
136
VocieProcess/rtc_base/system/file_wrapper.cc
Normal file
136
VocieProcess/rtc_base/system/file_wrapper.cc
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) 2012 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/system/file_wrapper.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/numerics/safe_conversions.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#endif
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
FILE* FileOpen(absl::string_view file_name_utf8, bool read_only, int* error) {
|
||||
RTC_CHECK_EQ(file_name_utf8.find_first_of('\0'), absl::string_view::npos)
|
||||
<< "Invalid filename, containing NUL character";
|
||||
std::string file_name(file_name_utf8);
|
||||
#if defined(_WIN32)
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, nullptr, 0);
|
||||
std::wstring wstr(len, 0);
|
||||
MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, &wstr[0], len);
|
||||
FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb");
|
||||
#else
|
||||
FILE* file = fopen(file_name.c_str(), read_only ? "rb" : "wb");
|
||||
#endif
|
||||
if (!file && error) {
|
||||
*error = errno;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
FileWrapper FileWrapper::OpenReadOnly(absl::string_view file_name_utf8) {
|
||||
return FileWrapper(FileOpen(file_name_utf8, true, nullptr));
|
||||
}
|
||||
|
||||
// static
|
||||
FileWrapper FileWrapper::OpenWriteOnly(absl::string_view file_name_utf8,
|
||||
int* error /*=nullptr*/) {
|
||||
return FileWrapper(FileOpen(file_name_utf8, false, error));
|
||||
}
|
||||
|
||||
FileWrapper::FileWrapper(FileWrapper&& other) {
|
||||
operator=(std::move(other));
|
||||
}
|
||||
|
||||
FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
|
||||
Close();
|
||||
file_ = other.file_;
|
||||
other.file_ = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool FileWrapper::SeekRelative(int64_t offset) {
|
||||
RTC_DCHECK(file_);
|
||||
return fseek(file_, rtc::checked_cast<long>(offset), SEEK_CUR) == 0;
|
||||
}
|
||||
|
||||
bool FileWrapper::SeekTo(int64_t position) {
|
||||
RTC_DCHECK(file_);
|
||||
return fseek(file_, rtc::checked_cast<long>(position), SEEK_SET) == 0;
|
||||
}
|
||||
|
||||
absl::optional<size_t> FileWrapper::FileSize() {
|
||||
if (file_ == nullptr)
|
||||
return absl::nullopt;
|
||||
long original_position = ftell(file_);
|
||||
if (original_position < 0)
|
||||
return absl::nullopt;
|
||||
int seek_error = fseek(file_, 0, SEEK_END);
|
||||
if (seek_error)
|
||||
return absl::nullopt;
|
||||
long file_size = ftell(file_);
|
||||
seek_error = fseek(file_, original_position, SEEK_SET);
|
||||
if (seek_error)
|
||||
return absl::nullopt;
|
||||
return rtc::checked_cast<size_t>(file_size);
|
||||
}
|
||||
|
||||
bool FileWrapper::Flush() {
|
||||
RTC_DCHECK(file_);
|
||||
return fflush(file_) == 0;
|
||||
}
|
||||
|
||||
size_t FileWrapper::Read(void* buf, size_t length) {
|
||||
RTC_DCHECK(file_);
|
||||
return fread(buf, 1, length, file_);
|
||||
}
|
||||
|
||||
bool FileWrapper::ReadEof() const {
|
||||
RTC_DCHECK(file_);
|
||||
return feof(file_);
|
||||
}
|
||||
|
||||
bool FileWrapper::Write(const void* buf, size_t length) {
|
||||
RTC_DCHECK(file_);
|
||||
return fwrite(buf, 1, length, file_) == length;
|
||||
}
|
||||
|
||||
bool FileWrapper::Close() {
|
||||
if (file_ == nullptr)
|
||||
return true;
|
||||
|
||||
bool success = fclose(file_) == 0;
|
||||
file_ = nullptr;
|
||||
return success;
|
||||
}
|
||||
|
||||
FILE* FileWrapper::Release() {
|
||||
FILE* file = file_;
|
||||
file_ = nullptr;
|
||||
return file;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
114
VocieProcess/rtc_base/system/file_wrapper.h
Normal file
114
VocieProcess/rtc_base/system/file_wrapper.h
Normal file
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2011 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_SYSTEM_FILE_WRAPPER_H_
|
||||
#define RTC_BASE_SYSTEM_FILE_WRAPPER_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/optional.h"
|
||||
|
||||
// Implementation that can read (exclusive) or write from/to a file.
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// This class is a thin wrapper around FILE*. It's main features are that it
|
||||
// owns the FILE*, calling fclose on destruction, and that on windows, file
|
||||
// names passed to the open methods are always treated as utf-8, regardless of
|
||||
// system code page.
|
||||
|
||||
// Most of the methods return only a success/fail indication. When needed, an
|
||||
// optional argument |int* error| should be added to all methods, in the same
|
||||
// way as for the OpenWriteOnly methods.
|
||||
class FileWrapper final {
|
||||
public:
|
||||
// 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. On failure,
|
||||
// and if `error` is non-null, the system errno value is stored at |*error|.
|
||||
// The file is closed by the destructor.
|
||||
static FileWrapper OpenReadOnly(absl::string_view file_name_utf8);
|
||||
static FileWrapper OpenWriteOnly(absl::string_view file_name_utf8,
|
||||
int* error = nullptr);
|
||||
|
||||
FileWrapper() = default;
|
||||
|
||||
// Takes over ownership of `file`, closing it on destruction. Calling with
|
||||
// null `file` is allowed, and results in a FileWrapper with is_open() false.
|
||||
explicit FileWrapper(FILE* file) : file_(file) {}
|
||||
~FileWrapper() { Close(); }
|
||||
|
||||
// Copying is not supported.
|
||||
FileWrapper(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();
|
||||
|
||||
// Releases and returns the wrapped file without closing it. This call passes
|
||||
// the ownership of the file to the caller, and the wrapper is no longer
|
||||
// responsible for closing it. Similarly the previously wrapped file is no
|
||||
// longer available for the wrapper to use in any aspect.
|
||||
FILE* Release();
|
||||
|
||||
// Write any buffered data to the underlying file. Returns true on success,
|
||||
// false on write error. Note: Flushing when closing, is not required.
|
||||
bool Flush();
|
||||
|
||||
// Seeks to the beginning of file. Returns true on success, false on failure,
|
||||
// e.g., if the underlying file isn't seekable.
|
||||
bool Rewind() { return SeekTo(0); }
|
||||
// TODO(nisse): The seek functions are used only by the WavReader. If that
|
||||
// code is demoted to test code, seek functions can be deleted from this
|
||||
// utility.
|
||||
// Seek relative to current file position.
|
||||
bool SeekRelative(int64_t offset);
|
||||
// Seek to given position.
|
||||
bool SeekTo(int64_t position);
|
||||
|
||||
// Returns the file size or -1 if a size could not be determined.
|
||||
// (A file size might not exists for non-seekable files or file-like
|
||||
// objects, for example /dev/tty on unix.)
|
||||
absl::optional<size_t> FileSize();
|
||||
|
||||
// Returns number of bytes read. Short count indicates EOF or error.
|
||||
size_t Read(void* buf, size_t length);
|
||||
|
||||
// If the most recent Read() returned a short count, this methods returns true
|
||||
// if the short count was due to EOF, and false it it was due to some i/o
|
||||
// error.
|
||||
bool ReadEof() const;
|
||||
|
||||
// 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
|
||||
|
||||
#endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_
|
Reference in New Issue
Block a user