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}
diff --git a/common_audio/BUILD.gn b/common_audio/BUILD.gn
index 4729e13..0dd5ec3 100644
--- a/common_audio/BUILD.gn
+++ b/common_audio/BUILD.gn
@@ -54,6 +54,7 @@
     "../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 2170b7e..b404d93 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 @@
                      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 @@
   // 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 @@
 #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::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 1e32bf7..87086a4 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 @@
   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 @@
   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 726552a..d7b09c1 100644
--- a/common_audio/wav_file_unittest.cc
+++ b/common_audio/wav_file_unittest.cc
@@ -151,7 +151,7 @@
   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());