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