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 "chrome/browser/media/webrtc_browsertest_audio.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/files/file.h" | |
10 #include "base/files/file_path.h" | |
11 #include "media/audio/audio_power_monitor.h" | |
12 #include "media/audio/sounds/wav_audio_handler.h" | |
13 #include "media/base/audio_bus.h" | |
14 #include "media/base/audio_parameters.h" | |
15 | |
16 namespace { | |
17 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | |
18 // bluntly trigger CHECKs if we can't read the file or if it's malformed. The | |
19 // caller takes ownership of the returned data. The size of the data is stored | |
20 // in |read_length|. | |
21 std::unique_ptr<char[]> ReadWavFile(const base::FilePath& wav_filename, | |
22 size_t* file_length) { | |
23 base::File wav_file( | |
24 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
25 if (!wav_file.IsValid()) { | |
26 CHECK(false) << "Failed to read " << wav_filename.value(); | |
27 return nullptr; | |
28 } | |
29 | |
30 size_t wav_file_length = wav_file.GetLength(); | |
31 | |
32 std::unique_ptr<char[]> data(new char[wav_file_length]); | |
33 size_t read_bytes = wav_file.Read(0, data.get(), wav_file_length); | |
34 if (read_bytes != wav_file_length) { | |
35 LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value(); | |
36 return nullptr; | |
37 } | |
38 *file_length = wav_file_length; | |
39 return data; | |
40 } | |
41 } // namespace | |
42 | |
43 namespace test { | |
44 | |
45 float ComputeAudioEnergyForWavFile(const base::FilePath& wav_filename, | |
46 media::AudioParameters* file_parameters) { | |
47 // Read the file, and put its data in a scoped_ptr so it gets deleted later. | |
48 size_t file_length = 0; | |
49 std::unique_ptr<char[]> wav_file_data = | |
50 ReadWavFile(wav_filename, &file_length); | |
51 auto wav_audio_handler = media::WavAudioHandler::Create( | |
52 base::StringPiece(wav_file_data.get(), file_length)); | |
53 | |
54 std::unique_ptr<media::AudioBus> audio_bus = media::AudioBus::Create( | |
55 wav_audio_handler->num_channels(), wav_audio_handler->total_frames()); | |
56 base::TimeDelta file_duration = wav_audio_handler->GetDuration(); | |
57 | |
58 size_t bytes_written; | |
59 wav_audio_handler->CopyTo(audio_bus.get(), 0, &bytes_written); | |
60 CHECK_EQ(bytes_written, wav_audio_handler->data().size()) | |
61 << "Expected to write entire file into bus."; | |
62 | |
63 // Set the filter coefficient to the whole file's duration; this will make the | |
64 // power monitor take the entire file into account. | |
65 media::AudioPowerMonitor power_monitor(wav_audio_handler->sample_rate(), | |
66 file_duration); | |
67 power_monitor.Scan(*audio_bus, audio_bus->frames()); | |
68 | |
69 file_parameters->Reset( | |
70 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
71 media::GuessChannelLayout(wav_audio_handler->num_channels()), | |
72 wav_audio_handler->sample_rate(), wav_audio_handler->bits_per_sample(), | |
73 wav_audio_handler->total_frames()); | |
74 file_parameters->set_channels_for_discrete(wav_audio_handler->num_channels()); | |
75 | |
76 return power_monitor.ReadCurrentPowerAndClip().first; | |
77 } | |
78 | |
79 } // namespace test | |
OLD | NEW |