Refactor WavWriter to use FileWrapper rather than PlatformFile
Bug: webrtc:6463 Change-Id: I4c80995481ed7d5c1079450d04ed7958fa137e84 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/141662 Reviewed-by: Henrik Grunell <henrikg@webrtc.org> Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org> Commit-Queue: Niels Moller <nisse@webrtc.org> Cr-Commit-Position: refs/heads/master@{#28279}
This commit is contained in:
parent
04e129ab1d
commit
19a1d50ccf
@ -54,6 +54,7 @@ rtc_static_library("common_audio") {
|
||||
"../rtc_base/memory:aligned_array",
|
||||
"../rtc_base/memory:aligned_malloc",
|
||||
"../rtc_base/system:arch",
|
||||
"../rtc_base/system:file_wrapper",
|
||||
"../system_wrappers",
|
||||
"../system_wrappers:cpu_features_api",
|
||||
"third_party/fft4g",
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "common_audio/include/audio_util.h"
|
||||
#include "common_audio/wav_header.h"
|
||||
@ -139,25 +140,17 @@ WavWriter::WavWriter(const std::string& filename,
|
||||
size_t num_channels)
|
||||
// Unlike plain fopen, CreatePlatformFile takes care of filename utf8 ->
|
||||
// wchar conversion on windows.
|
||||
: WavWriter(rtc::CreatePlatformFile(filename), sample_rate, num_channels) {}
|
||||
: WavWriter(FileWrapper::OpenWriteOnly(filename),
|
||||
sample_rate,
|
||||
num_channels) {}
|
||||
|
||||
WavWriter::WavWriter(rtc::PlatformFile file,
|
||||
int sample_rate,
|
||||
size_t num_channels)
|
||||
: sample_rate_(sample_rate), num_channels_(num_channels), num_samples_(0) {
|
||||
WavWriter::WavWriter(FileWrapper file, int sample_rate, size_t num_channels)
|
||||
: sample_rate_(sample_rate),
|
||||
num_channels_(num_channels),
|
||||
num_samples_(0),
|
||||
file_(std::move(file)) {
|
||||
// Handle errors from the CreatePlatformFile call in above constructor.
|
||||
RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
|
||||
<< "Invalid file. Could not create wav file.";
|
||||
file_handle_ = rtc::FdopenPlatformFile(file, "wb");
|
||||
if (!file_handle_) {
|
||||
RTC_LOG(LS_ERROR) << "Could not open wav file for writing.";
|
||||
// Even though we failed to open a FILE*, the file is still open
|
||||
// and needs to be closed.
|
||||
if (!rtc::ClosePlatformFile(file)) {
|
||||
RTC_LOG(LS_ERROR) << "Can't close file.";
|
||||
}
|
||||
FATAL() << "Could not open wav file for writing.";
|
||||
}
|
||||
RTC_CHECK(file_.is_open()) << "Invalid file. Could not create wav file.";
|
||||
|
||||
RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
|
||||
kBytesPerSample, num_samples_));
|
||||
@ -165,7 +158,7 @@ WavWriter::WavWriter(rtc::PlatformFile file,
|
||||
// Write a blank placeholder header, since we need to know the total number
|
||||
// of samples before we can fill in the real data.
|
||||
static const uint8_t blank_header[kWavHeaderSize] = {0};
|
||||
RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_));
|
||||
RTC_CHECK(file_.Write(blank_header, kWavHeaderSize));
|
||||
}
|
||||
|
||||
WavWriter::~WavWriter() {
|
||||
@ -188,11 +181,9 @@ void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
|
||||
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
|
||||
#error "Need to convert samples to little-endian when writing to WAV file"
|
||||
#endif
|
||||
const size_t written =
|
||||
fwrite(samples, sizeof(*samples), num_samples, file_handle_);
|
||||
RTC_CHECK_EQ(num_samples, written);
|
||||
num_samples_ += written;
|
||||
RTC_CHECK(num_samples_ >= written); // detect size_t overflow
|
||||
RTC_CHECK(file_.Write(samples, sizeof(*samples) * num_samples));
|
||||
num_samples_ += num_samples;
|
||||
RTC_CHECK(num_samples_ >= num_samples); // detect size_t overflow
|
||||
}
|
||||
|
||||
void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
|
||||
@ -206,13 +197,12 @@ void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
|
||||
}
|
||||
|
||||
void WavWriter::Close() {
|
||||
RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET));
|
||||
RTC_CHECK(file_.Rewind());
|
||||
uint8_t header[kWavHeaderSize];
|
||||
WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat,
|
||||
kBytesPerSample, num_samples_);
|
||||
RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_));
|
||||
RTC_CHECK_EQ(0, fclose(file_handle_));
|
||||
file_handle_ = nullptr;
|
||||
RTC_CHECK(file_.Write(header, kWavHeaderSize));
|
||||
RTC_CHECK(file_.Close());
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
|
||||
#include "rtc_base/constructor_magic.h"
|
||||
#include "rtc_base/platform_file.h"
|
||||
#include "rtc_base/system/file_wrapper.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
@ -38,7 +39,7 @@ class WavWriter final : public WavFile {
|
||||
WavWriter(const std::string& filename, int sample_rate, size_t num_channels);
|
||||
|
||||
// Open a new WAV file for writing.
|
||||
WavWriter(rtc::PlatformFile file, int sample_rate, size_t num_channels);
|
||||
WavWriter(FileWrapper file, int sample_rate, size_t num_channels);
|
||||
|
||||
// Close the WAV file, after writing its header.
|
||||
~WavWriter() override;
|
||||
@ -58,7 +59,7 @@ class WavWriter final : public WavFile {
|
||||
const int sample_rate_;
|
||||
const size_t num_channels_;
|
||||
size_t num_samples_; // Total number of samples written to file.
|
||||
FILE* file_handle_; // Output file, owned by this class
|
||||
FileWrapper file_; // Output file, owned by this class
|
||||
|
||||
RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter);
|
||||
};
|
||||
|
||||
@ -151,7 +151,7 @@ TEST(WavWriterTest, MAYBE_CPPFileDescriptor) {
|
||||
const std::string outfile = test::OutputPath() + "wavtest1.wav";
|
||||
static constexpr size_t kNumSamples = 3;
|
||||
{
|
||||
WavWriter w(rtc::CreatePlatformFile(outfile), 14099, 1);
|
||||
WavWriter w(FileWrapper::OpenWriteOnly(outfile), 14099, 1);
|
||||
EXPECT_EQ(14099, w.sample_rate());
|
||||
EXPECT_EQ(1u, w.num_channels());
|
||||
EXPECT_EQ(0u, w.num_samples());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user