OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webrtc_browsertest_audio.h" |
| 6 |
| 7 #include "base/files/file.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "media/audio/audio_power_monitor.h" |
| 10 #include "media/audio/sounds/wav_audio_handler.h" |
| 11 #include "media/base/audio_bus.h" |
| 12 |
| 13 namespace { |
| 14 // Opens |wav_filename|, reads it and loads it as a wav file. This function will |
| 15 // bluntly trigger CHECKs if we can't read the file or if it's malformed. The |
| 16 // caller takes ownership of the returned data. The size of the data is stored |
| 17 // in |read_length|. |
| 18 scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename, |
| 19 size_t* file_length) { |
| 20 base::File wav_file( |
| 21 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 22 if (!wav_file.IsValid()) { |
| 23 CHECK(false) << "Failed to read " << wav_filename.value(); |
| 24 return nullptr; |
| 25 } |
| 26 |
| 27 size_t wav_file_length = wav_file.GetLength(); |
| 28 |
| 29 uint8* wav_file_data = new uint8[wav_file_length]; |
| 30 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data), |
| 31 wav_file_length); |
| 32 if (read_bytes != wav_file_length) { |
| 33 CHECK(false) << "Failed to read all bytes of " << wav_filename.value(); |
| 34 return nullptr; |
| 35 } |
| 36 *file_length = wav_file_length; |
| 37 return scoped_ptr<uint8[]>(wav_file_data); |
| 38 } |
| 39 |
| 40 // Opens |wav_filename|, reads it and loads it as a Wav file. This function will |
| 41 // bluntly trigger CHECKs if the file doesn't have the sampling frequency, bits |
| 42 // per sample or number of channels as specified in |expected_params|. We also |
| 43 // trigger CHECKs if we can't read the file or if it's malformed. |
| 44 scoped_ptr<media::WavAudioHandler> CreateWavAudioHandler( |
| 45 const base::FilePath& wav_filename, const uint8* wav_file_data, |
| 46 size_t wav_file_length) { |
| 47 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data), |
| 48 wav_file_length); |
| 49 scoped_ptr<media::WavAudioHandler> wav_audio_handler( |
| 50 new media::WavAudioHandler(wav_data)); |
| 51 |
| 52 return wav_audio_handler.Pass(); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 namespace test { |
| 58 |
| 59 enum { kPowerMeasurementTimeConstantMillis = 10 }; |
| 60 |
| 61 std::vector<float> ComputeAudioEnergyForWavFile( |
| 62 const base::FilePath& wav_filename) { |
| 63 // Read the file, and put its data in a scoped_ptr so it gets deleted later. |
| 64 size_t file_length = 0; |
| 65 scoped_ptr<uint8[]> wav_file_data = ReadWavFile(wav_filename, &file_length); |
| 66 scoped_ptr<media::WavAudioHandler> wav_audio_handler = CreateWavAudioHandler( |
| 67 wav_filename, wav_file_data.get(), file_length); |
| 68 |
| 69 int num_frames_in_10_ms = |
| 70 10 * wav_audio_handler->params().sample_rate() / 1000; |
| 71 media::AudioParameters bus_params = media::AudioParameters( |
| 72 wav_audio_handler->params().format(), |
| 73 wav_audio_handler->params().channel_layout(), |
| 74 wav_audio_handler->params().sample_rate(), |
| 75 wav_audio_handler->params().bits_per_sample(), |
| 76 num_frames_in_10_ms); |
| 77 |
| 78 scoped_ptr<media::AudioBus> audio_bus = |
| 79 media::AudioBus::Create(bus_params); |
| 80 media::AudioPowerMonitor power_monitor( |
| 81 wav_audio_handler->params().sample_rate(), |
| 82 base::TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMillis)); |
| 83 |
| 84 int read_pos = 0; |
| 85 size_t bytes_written; |
| 86 std::vector<float> results; |
| 87 |
| 88 while (!wav_audio_handler->AtEnd(read_pos)) { |
| 89 wav_audio_handler->CopyTo(audio_bus.get(), read_pos, &bytes_written); |
| 90 power_monitor.Scan(*audio_bus, audio_bus->frames()); |
| 91 results.push_back(power_monitor.ReadCurrentPowerAndClip().first); |
| 92 read_pos += bytes_written; |
| 93 } |
| 94 |
| 95 return results; |
| 96 } |
| 97 |
| 98 } // namespace test |
OLD | NEW |