Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: media/audio/simple_sources.cc

Issue 1453233002: Improve input handling for WaveAudioHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Correct typo Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // MSVC++ requires this to be set before any other includes to get M_PI. 4 // MSVC++ requires this to be set before any other includes to get M_PI.
5 #define _USE_MATH_DEFINES 5 #define _USE_MATH_DEFINES
6 #include <cmath> 6 #include <cmath>
7 7
8 #include "media/audio/simple_sources.h" 8 #include "media/audio/simple_sources.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data), 42 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data),
43 wav_file_length); 43 wav_file_length);
44 if (read_bytes != wav_file_length) { 44 if (read_bytes != wav_file_length) {
45 LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value(); 45 LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value();
46 return nullptr; 46 return nullptr;
47 } 47 }
48 *file_length = wav_file_length; 48 *file_length = wav_file_length;
49 return scoped_ptr<uint8[]>(wav_file_data); 49 return scoped_ptr<uint8[]>(wav_file_data);
50 } 50 }
51 51
52 // Opens |wav_filename|, reads it and loads it as a wav file. This function will 52 // Opens |wav_filename|, reads it and loads it as a wav file.
53 // bluntly trigger CHECKs if we can't read the file or if it's malformed.
54 static scoped_ptr<WavAudioHandler> CreateWavAudioHandler( 53 static scoped_ptr<WavAudioHandler> CreateWavAudioHandler(
55 const base::FilePath& wav_filename, const uint8* wav_file_data, 54 const base::FilePath& wav_filename, const uint8* wav_file_data,
56 size_t wav_file_length, const AudioParameters& expected_params) { 55 size_t wav_file_length, const AudioParameters& expected_params) {
57 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data), 56 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data),
58 wav_file_length); 57 wav_file_length);
59 scoped_ptr<WavAudioHandler> wav_audio_handler(new WavAudioHandler(wav_data)); 58 scoped_ptr<WavAudioHandler> wav_audio_handler(new WavAudioHandler(wav_data));
60 return wav_audio_handler.Pass(); 59 return wav_audio_handler.Pass();
61 } 60 }
62 61
63 // These values are based on experiments for local-to-local 62 // These values are based on experiments for local-to-local
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 return; 171 return;
173 172
174 // Read the file, and put its data in a scoped_ptr so it gets deleted later. 173 // Read the file, and put its data in a scoped_ptr so it gets deleted later.
175 size_t file_length = 0; 174 size_t file_length = 0;
176 wav_file_data_ = ReadWavFile(path_to_wav_file, &file_length); 175 wav_file_data_ = ReadWavFile(path_to_wav_file, &file_length);
177 if (!wav_file_data_) { 176 if (!wav_file_data_) {
178 load_failed_ = true; 177 load_failed_ = true;
179 return; 178 return;
180 } 179 }
181 180
181 // Attempt to create a handler with this data. If the data is invalid, return.
182 wav_audio_handler_ = CreateWavAudioHandler( 182 wav_audio_handler_ = CreateWavAudioHandler(
183 path_to_wav_file, wav_file_data_.get(), file_length, params_); 183 path_to_wav_file, wav_file_data_.get(), file_length, params_);
184 if (!wav_audio_handler_->is_valid()) {
185 LOG(ERROR) << "WAV data could be read but is not valid";
186 wav_audio_handler_.reset();
187 load_failed_ = true;
188 return;
189 }
184 190
185 // Hook us up so we pull in data from the file into the converter. We need to 191 // Hook us up so we pull in data from the file into the converter. We need to
186 // modify the wav file's audio parameters since we'll be reading small slices 192 // modify the wav file's audio parameters since we'll be reading small slices
187 // of it at a time and not the whole thing (like 10 ms at a time). 193 // of it at a time and not the whole thing (like 10 ms at a time).
188 AudioParameters file_audio_slice( 194 AudioParameters file_audio_slice(
189 AudioParameters::AUDIO_PCM_LOW_LATENCY, 195 AudioParameters::AUDIO_PCM_LOW_LATENCY,
190 GuessChannelLayout(wav_audio_handler_->num_channels()), 196 GuessChannelLayout(wav_audio_handler_->num_channels()),
191 wav_audio_handler_->sample_rate(), wav_audio_handler_->bits_per_sample(), 197 wav_audio_handler_->sample_rate(), wav_audio_handler_->bits_per_sample(),
192 params_.frames_per_buffer()); 198 params_.frames_per_buffer());
193 199
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 301 }
296 302
297 void BeepingSource::OnError(AudioOutputStream* stream) { 303 void BeepingSource::OnError(AudioOutputStream* stream) {
298 } 304 }
299 305
300 void BeepingSource::BeepOnce() { 306 void BeepingSource::BeepOnce() {
301 g_beep_context.Pointer()->SetBeepOnce(true); 307 g_beep_context.Pointer()->SetBeepOnce(true);
302 } 308 }
303 309
304 } // namespace media 310 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698