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/webrtc_browsertest_audio.h" | 5 #include "chrome/browser/media/webrtc/webrtc_browsertest_audio.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "media/audio/audio_power_monitor.h" | 11 #include "media/audio/audio_power_monitor.h" |
12 #include "media/audio/sounds/wav_audio_handler.h" | 12 #include "media/audio/sounds/wav_audio_handler.h" |
13 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
14 #include "media/base/audio_parameters.h" | 14 #include "media/base/audio_parameters.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | 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 | 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 | 19 // caller takes ownership of the returned data. The size of the data is stored |
20 // in |read_length|. | 20 // in |read_length|. |
21 std::unique_ptr<char[]> ReadWavFile(const base::FilePath& wav_filename, | 21 std::unique_ptr<char[]> ReadWavFile(const base::FilePath& wav_filename, |
22 size_t* file_length) { | 22 size_t* file_length) { |
23 base::File wav_file( | 23 base::File wav_file( |
24 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); | 24 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); |
25 if (!wav_file.IsValid()) { | 25 if (!wav_file.IsValid()) { |
26 CHECK(false) << "Failed to read " << wav_filename.value(); | 26 CHECK(false) << "Failed to read " << wav_filename.value(); |
27 return nullptr; | 27 return nullptr; |
28 } | 28 } |
29 | 29 |
30 size_t wav_file_length = wav_file.GetLength(); | 30 int64_t wav_file_length64 = wav_file.GetLength(); |
| 31 if (wav_file_length64 < 0) { |
| 32 PLOG(ERROR) << "GetLength " << wav_file.GetPlatformFile(); |
| 33 return nullptr; |
| 34 } |
| 35 if (wav_file_length64 > std::numeric_limits<int>::max()) { |
| 36 LOG(ERROR) << "File is too big: " << wav_filename.value(); |
| 37 return nullptr; |
| 38 } |
| 39 int wav_file_length = static_cast<int>(wav_file_length64); |
| 40 if (!wav_file_length) { |
| 41 LOG(ERROR) << "Input file is empty: " << wav_filename.value(); |
| 42 return nullptr; |
| 43 } |
31 | 44 |
32 std::unique_ptr<char[]> data(new char[wav_file_length]); | 45 std::unique_ptr<char[]> data(new char[wav_file_length]); |
33 size_t read_bytes = wav_file.Read(0, data.get(), wav_file_length); | 46 int read_bytes = wav_file.Read(0, data.get(), wav_file_length); |
34 if (read_bytes != wav_file_length) { | 47 if (read_bytes != wav_file_length) { |
35 LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value(); | 48 LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value(); |
36 return nullptr; | 49 return nullptr; |
37 } | 50 } |
38 *file_length = wav_file_length; | 51 *file_length = wav_file_length; |
39 return data; | 52 return data; |
40 } | 53 } |
41 } // namespace | 54 } // namespace |
42 | 55 |
43 namespace test { | 56 namespace test { |
(...skipping 26 matching lines...) Expand all Loading... |
70 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 83 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
71 media::GuessChannelLayout(wav_audio_handler->num_channels()), | 84 media::GuessChannelLayout(wav_audio_handler->num_channels()), |
72 wav_audio_handler->sample_rate(), wav_audio_handler->bits_per_sample(), | 85 wav_audio_handler->sample_rate(), wav_audio_handler->bits_per_sample(), |
73 wav_audio_handler->total_frames()); | 86 wav_audio_handler->total_frames()); |
74 file_parameters->set_channels_for_discrete(wav_audio_handler->num_channels()); | 87 file_parameters->set_channels_for_discrete(wav_audio_handler->num_channels()); |
75 | 88 |
76 return power_monitor.ReadCurrentPowerAndClip().first; | 89 return power_monitor.ReadCurrentPowerAndClip().first; |
77 } | 90 } |
78 | 91 |
79 } // namespace test | 92 } // namespace test |
OLD | NEW |