aboutsummaryrefslogtreecommitdiff
path: root/common_audio
diff options
context:
space:
mode:
authorNiels Möller <nisse@webrtc.org>2019-06-13 14:08:24 +0200
committerCommit Bot <commit-bot@chromium.org>2019-06-14 10:18:28 +0000
commit19a1d50ccfa5a8da5704cdb3f7e715562d6ce6c2 (patch)
tree85d84b70c4d7704edce3c4c3d1be7b111f809728 /common_audio
parent04e129ab1dca4ebe71f71241c2eb5cf3dc5f162c (diff)
downloadwebrtc-19a1d50ccfa5a8da5704cdb3f7e715562d6ce6c2.tar.gz
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}
Diffstat (limited to 'common_audio')
-rw-r--r--common_audio/BUILD.gn1
-rw-r--r--common_audio/wav_file.cc46
-rw-r--r--common_audio/wav_file.h5
-rw-r--r--common_audio/wav_file_unittest.cc2
4 files changed, 23 insertions, 31 deletions
diff --git a/common_audio/BUILD.gn b/common_audio/BUILD.gn
index 4729e13259..0dd5ec38ef 100644
--- a/common_audio/BUILD.gn
+++ b/common_audio/BUILD.gn
@@ -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",
diff --git a/common_audio/wav_file.cc b/common_audio/wav_file.cc
index 2170b7e4f9..b404d9392f 100644
--- a/common_audio/wav_file.cc
+++ b/common_audio/wav_file.cc
@@ -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::WavWriter(rtc::PlatformFile file,
- int sample_rate,
- size_t num_channels)
- : sample_rate_(sample_rate), num_channels_(num_channels), num_samples_(0) {
+ : WavWriter(FileWrapper::OpenWriteOnly(filename),
+ sample_rate,
+ num_channels) {}
+
+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
diff --git a/common_audio/wav_file.h b/common_audio/wav_file.h
index 1e32bf7855..87086a4261 100644
--- a/common_audio/wav_file.h
+++ b/common_audio/wav_file.h
@@ -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);
};
diff --git a/common_audio/wav_file_unittest.cc b/common_audio/wav_file_unittest.cc
index 726552a011..d7b09c1193 100644
--- a/common_audio/wav_file_unittest.cc
+++ b/common_audio/wav_file_unittest.cc
@@ -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());