Extend WavReader and WavWriter API.
Add ability to read and write wav files using rtc::PlatformFile instead
of file name.
Bug: webrtc:8946
Change-Id: If18d9465f2155a33547f800edbdac45971a0e878
Reviewed-on: https://webrtc-review.googlesource.com/61424
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22497}
diff --git a/common_audio/wav_file.cc b/common_audio/wav_file.cc
index 37f249e..1217e40 100644
--- a/common_audio/wav_file.cc
+++ b/common_audio/wav_file.cc
@@ -18,6 +18,7 @@
#include "common_audio/include/audio_util.h"
#include "common_audio/wav_header.h"
#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
#include "rtc_base/numerics/safe_conversions.h"
namespace webrtc {
@@ -47,8 +48,21 @@
}
WavReader::WavReader(const std::string& filename)
- : file_handle_(fopen(filename.c_str(), "rb")) {
- RTC_CHECK(file_handle_) << "Could not open wav file for reading.";
+ : WavReader(rtc::OpenPlatformFileReadOnly(filename)) {}
+
+WavReader::WavReader(rtc::PlatformFile file) {
+ RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
+ << "Invalid file. Could not create file handle for wav file.";
+ file_handle_ = rtc::FdopenPlatformFile(file, "rb");
+ if (!file_handle_) {
+ RTC_LOG(LS_ERROR) << "Could not open wav file for reading: " << errno;
+ // 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 reading.";
+ }
ReadableWavFile readable(file_handle_);
WavFormat format;
@@ -110,13 +124,31 @@
file_handle_ = nullptr;
}
-WavWriter::WavWriter(const std::string& filename, int sample_rate,
+WavWriter::WavWriter(const std::string& filename,
+ int sample_rate,
size_t num_channels)
- : sample_rate_(sample_rate),
- num_channels_(num_channels),
- num_samples_(0),
- file_handle_(fopen(filename.c_str(), "wb")) {
- RTC_CHECK(file_handle_) << "Could not open wav file for writing.";
+ // 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) {
+ // 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(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
kBytesPerSample, num_samples_));