Commit 132678ee authored by phoglund's avatar phoglund Committed by Commit bot

Make fake file audio input device not crash when it fails to read.

The fake device used to just CHECK when it failed to read the file,
but I noticed those checks don't make it into the logs for official
builds. This makes the --use-file-for-fake-audio-capture flag a bit
user-hostile, which has also shown up in Chrome crash reports. This
way there will be an entry in the log and the device will be silent
instead of crashing.

BUG=466408

Review URL: https://codereview.chromium.org/1018293002

Cr-Commit-Position: refs/heads/master@{#321365}
parent f5d6a704
......@@ -18,26 +18,33 @@
namespace media {
// Opens |wav_filename|, reads it and loads it as a wav file. This function will
// bluntly trigger CHECKs if we can't read the file or if it's malformed. The
// return a null pointer if we can't read the file or if it's malformed. The
// caller takes ownership of the returned data. The size of the data is stored
// in |read_length|.
static scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename,
size_t* file_length) {
base::File wav_file(
wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ);
CHECK(wav_file.IsValid())
<< "Failed to read " << wav_filename.value()
<< " as input to the fake device.";
if (!wav_file.IsValid()) {
LOG(ERROR) << "Failed to read " << wav_filename.value()
<< " as input to the fake device.";
return nullptr;
}
size_t wav_file_length = wav_file.GetLength();
CHECK_GT(wav_file_length, 0u)
<< "Input file to fake device is empty: " << wav_filename.value();
if (wav_file_length == 0u) {
LOG(ERROR) << "Input file to fake device is empty: "
<< wav_filename.value();
return nullptr;
}
uint8* wav_file_data = new uint8[wav_file_length];
size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data),
wav_file_length);
CHECK_EQ(read_bytes, wav_file_length)
<< "Failed to read all bytes of " << wav_filename.value();
if (read_bytes != wav_file_length) {
LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value();
return nullptr;
}
*file_length = wav_file_length;
return scoped_ptr<uint8[]>(wav_file_data);
}
......@@ -152,16 +159,26 @@ FileSource::FileSource(const AudioParameters& params,
const base::FilePath& path_to_wav_file)
: params_(params),
path_to_wav_file_(path_to_wav_file),
wav_file_read_pos_(0) {
wav_file_read_pos_(0),
load_failed_(false) {
}
FileSource::~FileSource() {
}
void FileSource::LoadWavFile(const base::FilePath& path_to_wav_file) {
// Don't try again if we already failed.
if (load_failed_)
return;
// Read the file, and put its data in a scoped_ptr so it gets deleted later.
size_t file_length = 0;
wav_file_data_ = ReadWavFile(path_to_wav_file, &file_length);
if (!wav_file_data_) {
load_failed_ = true;
return;
}
wav_audio_handler_ = CreateWavAudioHandler(
path_to_wav_file, wav_file_data_.get(), file_length, params_);
......@@ -186,6 +203,8 @@ int FileSource::OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) {
// This will massively delay the first OnMoreData, but we'll catch up.
if (!wav_audio_handler_)
LoadWavFile(path_to_wav_file_);
if (load_failed_)
return 0;
DCHECK(wav_audio_handler_.get());
......
......@@ -66,6 +66,7 @@ class FileSource : public AudioOutputStream::AudioSourceCallback,
scoped_ptr<WavAudioHandler> wav_audio_handler_;
scoped_ptr<AudioConverter> file_audio_converter_;
int wav_file_read_pos_;
bool load_failed_;
// Provides audio data from wav_audio_handler_ into the file audio converter.
double ProvideInput(AudioBus* audio_bus,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment