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/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