Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / media / webrtc_browsertest_audio.cc
blob1f7defec05711daa6034e78cbd1967d0ab2bfd3c
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.
5 #include "chrome/browser/media/webrtc_browsertest_audio.h"
7 #include "base/files/file.h"
8 #include "base/files/file_path.h"
9 #include "media/audio/audio_parameters.h"
10 #include "media/audio/audio_power_monitor.h"
11 #include "media/audio/sounds/wav_audio_handler.h"
12 #include "media/base/audio_bus.h"
14 namespace {
15 // Opens |wav_filename|, reads it and loads it as a wav file. This function will
16 // bluntly trigger CHECKs if we can't read the file or if it's malformed. The
17 // caller takes ownership of the returned data. The size of the data is stored
18 // in |read_length|.
19 scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename,
20 size_t* file_length) {
21 base::File wav_file(
22 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ);
23 if (!wav_file.IsValid()) {
24 CHECK(false) << "Failed to read " << wav_filename.value();
25 return nullptr;
28 size_t wav_file_length = wav_file.GetLength();
30 uint8* wav_file_data = new uint8[wav_file_length];
31 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data),
32 wav_file_length);
33 if (read_bytes != wav_file_length) {
34 CHECK(false) << "Failed to read all bytes of " << wav_filename.value();
35 return nullptr;
37 *file_length = wav_file_length;
38 return scoped_ptr<uint8[]>(wav_file_data);
41 scoped_ptr<media::WavAudioHandler> CreateWavAudioHandler(
42 const base::FilePath& wav_filename, const uint8* wav_file_data,
43 size_t wav_file_length) {
44 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data),
45 wav_file_length);
46 scoped_ptr<media::WavAudioHandler> wav_audio_handler(
47 new media::WavAudioHandler(wav_data));
49 return wav_audio_handler.Pass();
52 } // namespace
54 namespace test {
56 float ComputeAudioEnergyForWavFile(const base::FilePath& wav_filename,
57 media::AudioParameters* file_parameters) {
58 // Read the file, and put its data in a scoped_ptr so it gets deleted later.
59 size_t file_length = 0;
60 scoped_ptr<uint8[]> wav_file_data = ReadWavFile(wav_filename, &file_length);
61 scoped_ptr<media::WavAudioHandler> wav_audio_handler = CreateWavAudioHandler(
62 wav_filename, wav_file_data.get(), file_length);
64 scoped_ptr<media::AudioBus> audio_bus = media::AudioBus::Create(
65 wav_audio_handler->num_channels(), wav_audio_handler->total_frames());
66 base::TimeDelta file_duration = wav_audio_handler->GetDuration();
68 size_t bytes_written;
69 wav_audio_handler->CopyTo(audio_bus.get(), 0, &bytes_written);
70 CHECK_EQ(bytes_written, wav_audio_handler->data().size())
71 << "Expected to write entire file into bus.";
73 // Set the filter coefficient to the whole file's duration; this will make the
74 // power monitor take the entire file into account.
75 media::AudioPowerMonitor power_monitor(wav_audio_handler->sample_rate(),
76 file_duration);
77 power_monitor.Scan(*audio_bus, audio_bus->frames());
79 file_parameters->Reset(
80 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
81 media::GuessChannelLayout(wav_audio_handler->num_channels()),
82 wav_audio_handler->sample_rate(), wav_audio_handler->bits_per_sample(),
83 wav_audio_handler->total_frames());
84 file_parameters->set_channels_for_discrete(wav_audio_handler->num_channels());
86 return power_monitor.ReadCurrentPowerAndClip().first;
89 } // namespace test