OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // MSVC++ requires this to be set before any other includes to get M_PI. | 4 // MSVC++ requires this to be set before any other includes to get M_PI. |
5 #define _USE_MATH_DEFINES | 5 #define _USE_MATH_DEFINES |
6 #include <cmath> | 6 #include <cmath> |
7 | 7 |
8 #include "media/audio/simple_sources.h" | 8 #include "media/audio/simple_sources.h" |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "media/audio/sounds/wav_audio_handler.h" | 15 #include "media/audio/sounds/wav_audio_handler.h" |
16 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
17 | 17 |
18 namespace media { | 18 namespace media { |
19 | 19 |
20 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | 20 // Opens |wav_filename|, reads it and loads it as a wav file. This function will |
21 // return a null pointer if we can't read the file or if it's malformed. The | 21 // return a null pointer if we can't read the file or if it's malformed. The |
22 // caller takes ownership of the returned data. The size of the data is stored | 22 // caller takes ownership of the returned data. The size of the data is stored |
23 // in |read_length|. | 23 // in |read_length|. |
24 static scoped_ptr<uint8[]> ReadWavFile(const base::FilePath& wav_filename, | 24 bool ReadWavFile(base::StringPiece* data_out, |
25 size_t* file_length) { | 25 const base::FilePath& wav_filename) { |
26 DCHECK(data_out); | |
26 base::File wav_file( | 27 base::File wav_file( |
27 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); | 28 wav_filename, base::File::FLAG_OPEN | base::File::FLAG_READ); |
28 if (!wav_file.IsValid()) { | 29 if (!wav_file.IsValid()) { |
29 LOG(ERROR) << "Failed to read " << wav_filename.value() | 30 LOG(ERROR) << "Failed to read " << wav_filename.value() |
30 << " as input to the fake device."; | 31 << " as input to the fake device."; |
31 return nullptr; | 32 return false; |
32 } | 33 } |
33 | 34 |
34 size_t wav_file_length = wav_file.GetLength(); | 35 size_t wav_file_length = wav_file.GetLength(); |
35 if (wav_file_length == 0u) { | 36 if (wav_file_length == 0u) { |
36 LOG(ERROR) << "Input file to fake device is empty: " | 37 LOG(ERROR) << "Input file to fake device is empty: " |
37 << wav_filename.value(); | 38 << wav_filename.value(); |
38 return nullptr; | 39 return false; |
39 } | 40 } |
40 | 41 |
41 uint8* wav_file_data = new uint8[wav_file_length]; | 42 scoped_ptr<char> data = make_scoped_ptr(new char[wav_file_length]); |
42 size_t read_bytes = wav_file.Read(0, reinterpret_cast<char*>(wav_file_data), | 43 size_t read_bytes = wav_file.Read(0, data.get(), wav_file_length); |
43 wav_file_length); | |
44 if (read_bytes != wav_file_length) { | 44 if (read_bytes != wav_file_length) { |
45 LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value(); | 45 LOG(ERROR) << "Failed to read all bytes of " << wav_filename.value(); |
46 return nullptr; | 46 return false; |
47 } | 47 } |
48 *file_length = wav_file_length; | 48 data_out->set(data.release(), wav_file_length); |
tommi (sloooow) - chröme
2015/11/20 09:45:23
how is this memory freed?
DaleCurtis
2015/11/20 17:31:39
Drive by, StringPiece does not make a copy of the
tommi (sloooow) - chröme
2015/11/20 18:01:27
At the moment I don't think this is ever freed act
slan
2015/11/20 18:09:36
Thanks, Dale. This was a case of me not understand
| |
49 return scoped_ptr<uint8[]>(wav_file_data); | 49 return true; |
50 } | |
51 | |
52 // Opens |wav_filename|, reads it and loads it as a wav file. This function will | |
53 // bluntly trigger CHECKs if we can't read the file or if it's malformed. | |
54 static scoped_ptr<WavAudioHandler> CreateWavAudioHandler( | |
55 const base::FilePath& wav_filename, const uint8* wav_file_data, | |
56 size_t wav_file_length, const AudioParameters& expected_params) { | |
57 base::StringPiece wav_data(reinterpret_cast<const char*>(wav_file_data), | |
58 wav_file_length); | |
59 scoped_ptr<WavAudioHandler> wav_audio_handler(new WavAudioHandler(wav_data)); | |
60 return wav_audio_handler.Pass(); | |
61 } | 50 } |
62 | 51 |
63 // These values are based on experiments for local-to-local | 52 // These values are based on experiments for local-to-local |
64 // PeerConnection to demonstrate audio/video synchronization. | 53 // PeerConnection to demonstrate audio/video synchronization. |
65 static const int kBeepDurationMilliseconds = 20; | 54 static const int kBeepDurationMilliseconds = 20; |
66 static const int kBeepFrequency = 400; | 55 static const int kBeepFrequency = 400; |
67 | 56 |
68 // Intervals between two automatic beeps. | 57 // Intervals between two automatic beeps. |
69 static const int kAutomaticBeepIntervalInMs = 500; | 58 static const int kAutomaticBeepIntervalInMs = 500; |
70 | 59 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 | 154 |
166 FileSource::~FileSource() { | 155 FileSource::~FileSource() { |
167 } | 156 } |
168 | 157 |
169 void FileSource::LoadWavFile(const base::FilePath& path_to_wav_file) { | 158 void FileSource::LoadWavFile(const base::FilePath& path_to_wav_file) { |
170 // Don't try again if we already failed. | 159 // Don't try again if we already failed. |
171 if (load_failed_) | 160 if (load_failed_) |
172 return; | 161 return; |
173 | 162 |
174 // Read the file, and put its data in a scoped_ptr so it gets deleted later. | 163 // Read the file, and put its data in a scoped_ptr so it gets deleted later. |
175 size_t file_length = 0; | 164 base::StringPiece data; |
176 wav_file_data_ = ReadWavFile(path_to_wav_file, &file_length); | 165 if (!ReadWavFile(&data, path_to_wav_file)) { |
177 if (!wav_file_data_) { | |
178 load_failed_ = true; | 166 load_failed_ = true; |
179 return; | 167 return; |
180 } | 168 } |
181 | 169 |
182 wav_audio_handler_ = CreateWavAudioHandler( | 170 // Attempt to create a handler with this data. If the data is invalid, return. |
183 path_to_wav_file, wav_file_data_.get(), file_length, params_); | 171 wav_audio_handler_ = WavAudioHandler::Create(data); |
172 if (!wav_audio_handler_) { | |
173 LOG(ERROR) << "WAV data could be read but is not valid"; | |
174 load_failed_ = true; | |
175 return; | |
176 } | |
184 | 177 |
185 // Hook us up so we pull in data from the file into the converter. We need to | 178 // Hook us up so we pull in data from the file into the converter. We need to |
186 // modify the wav file's audio parameters since we'll be reading small slices | 179 // modify the wav file's audio parameters since we'll be reading small slices |
187 // of it at a time and not the whole thing (like 10 ms at a time). | 180 // of it at a time and not the whole thing (like 10 ms at a time). |
188 AudioParameters file_audio_slice( | 181 AudioParameters file_audio_slice( |
189 AudioParameters::AUDIO_PCM_LOW_LATENCY, | 182 AudioParameters::AUDIO_PCM_LOW_LATENCY, |
190 GuessChannelLayout(wav_audio_handler_->num_channels()), | 183 GuessChannelLayout(wav_audio_handler_->num_channels()), |
191 wav_audio_handler_->sample_rate(), wav_audio_handler_->bits_per_sample(), | 184 wav_audio_handler_->sample_rate(), wav_audio_handler_->bits_per_sample(), |
192 params_.frames_per_buffer()); | 185 params_.frames_per_buffer()); |
193 | 186 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 } | 288 } |
296 | 289 |
297 void BeepingSource::OnError(AudioOutputStream* stream) { | 290 void BeepingSource::OnError(AudioOutputStream* stream) { |
298 } | 291 } |
299 | 292 |
300 void BeepingSource::BeepOnce() { | 293 void BeepingSource::BeepOnce() { |
301 g_beep_context.Pointer()->SetBeepOnce(true); | 294 g_beep_context.Pointer()->SetBeepOnce(true); |
302 } | 295 } |
303 | 296 |
304 } // namespace media | 297 } // namespace media |
OLD | NEW |