Chromium Code Reviews| Index: chrome/browser/media/webrtc_browsertest_audio.cc |
| diff --git a/chrome/browser/media/webrtc_browsertest_audio.cc b/chrome/browser/media/webrtc_browsertest_audio.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4abb31045d22760bb08e258f7f556d4826138076 |
| --- /dev/null |
| +++ b/chrome/browser/media/webrtc_browsertest_audio.cc |
| @@ -0,0 +1,95 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "webrtc_browsertest_audio.h" |
| + |
| +#include "base/files/file.h" |
| +#include "base/files/file_path.h" |
| +#include "media/audio/audio_power_monitor.h" |
| +#include "media/audio/sounds/wav_audio_handler.h" |
| +#include "media/base/audio_bus.h" |
| + |
| +namespace { |
| +// 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 |
| +// caller takes ownership of the returned data. The size of the data is stored |
| +// in |read_length|. |
| +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); |
| + if (!wav_file.IsValid()) { |
| + CHECK(false) << "Failed to read " << wav_filename.value(); |
| + return nullptr; |
| + } |
| + |
| + size_t wav_file_length = wav_file.GetLength(); |
| + |
| + 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); |
| + if (read_bytes != wav_file_length) { |
| + CHECK(false) << "Failed to read all bytes of " << wav_filename.value(); |
| + return nullptr; |
| + } |
| + *file_length = wav_file_length; |
| + return scoped_ptr<uint8[]>(wav_file_data); |
| +} |
| + |
| +scoped_ptr<media::WavAudioHandler> CreateWavAudioHandler( |
| + const base::FilePath& wav_filename, const uint8* wav_file_data, |
| + size_t wav_file_length) { |
| + base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data), |
| + wav_file_length); |
| + scoped_ptr<media::WavAudioHandler> wav_audio_handler( |
| + new media::WavAudioHandler(wav_data)); |
| + |
| + return wav_audio_handler.Pass(); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace test { |
| + |
| +enum { kPowerMeasurementTimeConstantMillis = 10 }; |
| + |
| +std::vector<float> ComputeAudioEnergyForWavFile( |
| + const base::FilePath& wav_filename) { |
| + // Read the file, and put its data in a scoped_ptr so it gets deleted later. |
| + size_t file_length = 0; |
| + scoped_ptr<uint8[]> wav_file_data = ReadWavFile(wav_filename, &file_length); |
| + scoped_ptr<media::WavAudioHandler> wav_audio_handler = CreateWavAudioHandler( |
| + wav_filename, wav_file_data.get(), file_length); |
| + |
| + // Set up the bus to read 10 ms at a time. |
|
bjornv
2014/12/18 11:30:27
Could we set up the bus to read the entire segment
|
| + int num_frames_in_10_ms = |
| + 10 * wav_audio_handler->params().sample_rate() / 1000; |
| + media::AudioParameters bus_params = media::AudioParameters( |
| + wav_audio_handler->params().format(), |
| + wav_audio_handler->params().channel_layout(), |
| + wav_audio_handler->params().sample_rate(), |
| + wav_audio_handler->params().bits_per_sample(), |
| + num_frames_in_10_ms); |
| + |
| + scoped_ptr<media::AudioBus> audio_bus = |
| + media::AudioBus::Create(bus_params); |
| + media::AudioPowerMonitor power_monitor( |
| + wav_audio_handler->params().sample_rate(), |
| + base::TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMillis)); |
| + |
| + int read_pos = 0; |
| + std::vector<float> results; |
| + |
| + while (!wav_audio_handler->AtEnd(read_pos)) { |
| + size_t bytes_written; |
| + wav_audio_handler->CopyTo(audio_bus.get(), read_pos, &bytes_written); |
| + power_monitor.Scan(*audio_bus, audio_bus->frames()); |
| + results.push_back(power_monitor.ReadCurrentPowerAndClip().first); |
| + read_pos += bytes_written; |
| + } |
| + |
| + return results; |
| +} |
| + |
| +} // namespace test |