OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/renderer_host/media/audio_debug_file_writer.h" | 5 #include "media/audio/audio_debug_file_writer.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <array> | 8 #include <array> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
13 #include "base/sys_byteorder.h" | 14 #include "base/sys_byteorder.h" |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "media/base/audio_bus.h" | 15 #include "media/base/audio_bus.h" |
16 | 16 |
17 namespace content { | 17 namespace media { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Windows WAVE format header | 21 // Windows WAVE format header |
22 // Byte order: Little-endian | 22 // Byte order: Little-endian |
23 // Offset Length Content | 23 // Offset Length Content |
24 // 0 4 "RIFF" | 24 // 0 4 "RIFF" |
25 // 4 4 <file length - 8> | 25 // 4 4 <file length - 8> |
26 // 8 4 "WAVE" | 26 // 8 4 "WAVE" |
27 // 12 4 "fmt " | 27 // 12 4 "fmt " |
(...skipping 28 matching lines...) Expand all Loading... | |
56 public: | 56 public: |
57 CharBufferWriter(char* buf, int max_size) | 57 CharBufferWriter(char* buf, int max_size) |
58 : buf_(buf), max_size_(max_size), size_(0) {} | 58 : buf_(buf), max_size_(max_size), size_(0) {} |
59 | 59 |
60 void Write(const char* data, int data_size) { | 60 void Write(const char* data, int data_size) { |
61 CHECK_LE(size_ + data_size, max_size_); | 61 CHECK_LE(size_ + data_size, max_size_); |
62 memcpy(&buf_[size_], data, data_size); | 62 memcpy(&buf_[size_], data, data_size); |
63 size_ += data_size; | 63 size_ += data_size; |
64 } | 64 } |
65 | 65 |
66 void Write(const char(&data)[4]) { Write(static_cast<const char*>(data), 4); } | 66 void Write(const char (&data)[4]) { |
67 Write(static_cast<const char*>(data), 4); | |
68 } | |
67 | 69 |
68 void WriteLE16(uint16_t data) { | 70 void WriteLE16(uint16_t data) { |
69 uint16_t val = base::ByteSwapToLE16(data); | 71 uint16_t val = base::ByteSwapToLE16(data); |
70 Write(reinterpret_cast<const char*>(&val), sizeof(val)); | 72 Write(reinterpret_cast<const char*>(&val), sizeof(val)); |
71 } | 73 } |
72 | 74 |
73 void WriteLE32(uint32_t data) { | 75 void WriteLE32(uint32_t data) { |
74 uint32_t val = base::ByteSwapToLE32(data); | 76 uint32_t val = base::ByteSwapToLE32(data); |
75 Write(reinterpret_cast<const char*>(&val), sizeof(val)); | 77 Write(reinterpret_cast<const char*>(&val), sizeof(val)); |
76 } | 78 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 writer.WriteLE32(sample_rate); | 125 writer.WriteLE32(sample_rate); |
124 writer.WriteLE32(byte_rate); | 126 writer.WriteLE32(byte_rate); |
125 writer.WriteLE16(block_align); | 127 writer.WriteLE16(block_align); |
126 writer.WriteLE16(kBytesPerSample * 8); | 128 writer.WriteLE16(kBytesPerSample * 8); |
127 writer.Write(kData); | 129 writer.Write(kData); |
128 writer.WriteLE32(bytes_in_payload); | 130 writer.WriteLE32(bytes_in_payload); |
129 } | 131 } |
130 | 132 |
131 } // namespace | 133 } // namespace |
132 | 134 |
133 // Manages the debug recording file and writes to it. Can be created on any | 135 // Manages the debug recording file and writes to it. Can be created on any |
DaleCurtis
2017/02/21 18:00:58
Do we still need this inner class setup?
Henrik Grunell
2017/02/21 21:07:52
I don't think that has changed because of the move
| |
134 // thread. All the operations must be executed on FILE thread. Must be destroyed | 136 // thread. All the operations must be executed on |task_runner_|. Must be |
135 // on FILE thread. | 137 // destroyed on |task_runner_|. |
136 class AudioDebugFileWriter::AudioFileWriter { | 138 class AudioDebugFileWriter::AudioFileWriter { |
137 public: | 139 public: |
138 static AudioFileWriterUniquePtr Create(const base::FilePath& file_name, | 140 static AudioFileWriterUniquePtr Create( |
139 const media::AudioParameters& params); | 141 const base::FilePath& file_name, |
142 const AudioParameters& params, | |
143 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
140 | 144 |
141 ~AudioFileWriter(); | 145 ~AudioFileWriter(); |
142 | 146 |
143 // Write data from |data| to file. | 147 // Write data from |data| to file. |
144 void Write(const media::AudioBus* data); | 148 void Write(const AudioBus* data); |
149 | |
150 scoped_refptr<base::SingleThreadTaskRunner> task_runner() { | |
151 return task_runner_; | |
152 } | |
145 | 153 |
146 private: | 154 private: |
147 AudioFileWriter(const media::AudioParameters& params); | 155 AudioFileWriter(const AudioParameters& params, |
156 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
148 | 157 |
149 // Write wave header to file. Called on the FILE thread twice: on construction | 158 // Write wave header to file. Called on the |task_runner_| twice: on |
159 // construction | |
150 // of AudioFileWriter size of the wave data is unknown, so the header is | 160 // of AudioFileWriter size of the wave data is unknown, so the header is |
151 // written with zero sizes; then on destruction it is re-written with the | 161 // written with zero sizes; then on destruction it is re-written with the |
152 // actual size info accumulated throughout the object lifetime. | 162 // actual size info accumulated throughout the object lifetime. |
153 void WriteHeader(); | 163 void WriteHeader(); |
154 | 164 |
155 void CreateRecordingFile(const base::FilePath& file_name); | 165 void CreateRecordingFile(const base::FilePath& file_name); |
156 | 166 |
157 // The file to write to. | 167 // The file to write to. |
158 base::File file_; | 168 base::File file_; |
159 | 169 |
160 // Number of written samples. | 170 // Number of written samples. |
161 uint64_t samples_; | 171 uint64_t samples_; |
162 | 172 |
163 // Input audio parameters required to build wave header. | 173 // Input audio parameters required to build wave header. |
164 const media::AudioParameters params_; | 174 const AudioParameters params_; |
165 | 175 |
166 // Intermediate buffer to be written to file. Interleaved 16 bit audio data. | 176 // Intermediate buffer to be written to file. Interleaved 16 bit audio data. |
167 std::unique_ptr<int16_t[]> interleaved_data_; | 177 std::unique_ptr<int16_t[]> interleaved_data_; |
168 int interleaved_data_size_; | 178 int interleaved_data_size_; |
179 | |
180 // The task runner this class operates on. | |
181 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
169 }; | 182 }; |
170 | 183 |
184 // AudioFileWriter deleter. Inspired by | |
185 // content::BrowserThread::DeleteOnFileThread. | |
186 void AudioDebugFileWriter::DeleteOnFileThread::operator()( | |
187 AudioFileWriter* ptr) const { | |
188 if (!ptr->task_runner()->DeleteSoon(FROM_HERE, ptr)) { | |
189 #if defined(UNIT_TEST) | |
190 // Only logged under unit testing because leaks at shutdown | |
191 // are acceptable under normal circumstances. | |
192 LOG(ERROR) << "DeleteSoon failed for AudioDebugFileWriter::AudioFileWriter"; | |
193 #endif | |
194 } | |
195 } | |
196 | |
171 // static | 197 // static |
172 AudioDebugFileWriter::AudioFileWriterUniquePtr | 198 AudioDebugFileWriter::AudioFileWriterUniquePtr |
173 AudioDebugFileWriter::AudioFileWriter::Create( | 199 AudioDebugFileWriter::AudioFileWriter::Create( |
174 const base::FilePath& file_name, | 200 const base::FilePath& file_name, |
175 const media::AudioParameters& params) { | 201 const AudioParameters& params, |
176 AudioFileWriterUniquePtr file_writer(new AudioFileWriter(params)); | 202 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
203 AudioFileWriterUniquePtr file_writer( | |
204 new AudioFileWriter(params, task_runner)); | |
177 | 205 |
178 // base::Unretained is safe, because destructor is called on FILE thread or on | 206 // base::Unretained is safe, because destructor is called on |
179 // FILE message loop destruction. | 207 // |task_runner|. |
180 BrowserThread::PostTask( | 208 task_runner->PostTask( |
181 BrowserThread::FILE, FROM_HERE, | 209 FROM_HERE, base::Bind(&AudioFileWriter::CreateRecordingFile, |
182 base::Bind(&AudioFileWriter::CreateRecordingFile, | 210 base::Unretained(file_writer.get()), file_name)); |
183 base::Unretained(file_writer.get()), file_name)); | |
184 return file_writer; | 211 return file_writer; |
185 } | 212 } |
186 | 213 |
187 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( | 214 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( |
188 const media::AudioParameters& params) | 215 const AudioParameters& params, |
189 : samples_(0), params_(params), interleaved_data_size_(0) { | 216 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
217 : samples_(0), | |
218 params_(params), | |
219 interleaved_data_size_(0), | |
220 task_runner_(std::move(task_runner)) { | |
190 DCHECK_EQ(params.bits_per_sample(), kBytesPerSample * 8); | 221 DCHECK_EQ(params.bits_per_sample(), kBytesPerSample * 8); |
191 } | 222 } |
192 | 223 |
193 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { | 224 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { |
194 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 225 DCHECK(task_runner_->BelongsToCurrentThread()); |
195 if (file_.IsValid()) | 226 if (file_.IsValid()) |
196 WriteHeader(); | 227 WriteHeader(); |
197 } | 228 } |
198 | 229 |
199 void AudioDebugFileWriter::AudioFileWriter::Write( | 230 void AudioDebugFileWriter::AudioFileWriter::Write(const AudioBus* data) { |
200 const media::AudioBus* data) { | 231 DCHECK(task_runner_->BelongsToCurrentThread()); |
201 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
202 if (!file_.IsValid()) | 232 if (!file_.IsValid()) |
203 return; | 233 return; |
204 | 234 |
205 // Convert to 16 bit audio and write to file. | 235 // Convert to 16 bit audio and write to file. |
206 int data_size = data->frames() * data->channels(); | 236 int data_size = data->frames() * data->channels(); |
207 if (!interleaved_data_ || interleaved_data_size_ < data_size) { | 237 if (!interleaved_data_ || interleaved_data_size_ < data_size) { |
208 interleaved_data_.reset(new int16_t[data_size]); | 238 interleaved_data_.reset(new int16_t[data_size]); |
209 interleaved_data_size_ = data_size; | 239 interleaved_data_size_ = data_size; |
210 } | 240 } |
211 samples_ += data_size; | 241 samples_ += data_size; |
212 data->ToInterleaved(data->frames(), sizeof(interleaved_data_[0]), | 242 data->ToInterleaved(data->frames(), sizeof(interleaved_data_[0]), |
213 interleaved_data_.get()); | 243 interleaved_data_.get()); |
214 | 244 |
215 #ifndef ARCH_CPU_LITTLE_ENDIAN | 245 #ifndef ARCH_CPU_LITTLE_ENDIAN |
216 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), | 246 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), |
217 "Only 2 bytes per channel is supported."); | 247 "Only 2 bytes per channel is supported."); |
218 for (int i = 0; i < data_size; ++i) | 248 for (int i = 0; i < data_size; ++i) |
219 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); | 249 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); |
220 #endif | 250 #endif |
221 | 251 |
222 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), | 252 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), |
223 data_size * sizeof(interleaved_data_[0])); | 253 data_size * sizeof(interleaved_data_[0])); |
224 } | 254 } |
225 | 255 |
226 void AudioDebugFileWriter::AudioFileWriter::WriteHeader() { | 256 void AudioDebugFileWriter::AudioFileWriter::WriteHeader() { |
227 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 257 DCHECK(task_runner_->BelongsToCurrentThread()); |
228 if (!file_.IsValid()) | 258 if (!file_.IsValid()) |
229 return; | 259 return; |
230 WavHeaderBuffer buf; | 260 WavHeaderBuffer buf; |
231 WriteWavHeader(&buf, params_.channels(), params_.sample_rate(), samples_); | 261 WriteWavHeader(&buf, params_.channels(), params_.sample_rate(), samples_); |
232 file_.Write(0, &buf[0], kWavHeaderSize); | 262 file_.Write(0, &buf[0], kWavHeaderSize); |
233 | 263 |
234 // Write() does not move the cursor if file is not in APPEND mode; Seek() so | 264 // Write() does not move the cursor if file is not in APPEND mode; Seek() so |
235 // that the header is not overwritten by the following writes. | 265 // that the header is not overwritten by the following writes. |
236 file_.Seek(base::File::FROM_BEGIN, kWavHeaderSize); | 266 file_.Seek(base::File::FROM_BEGIN, kWavHeaderSize); |
237 } | 267 } |
238 | 268 |
239 void AudioDebugFileWriter::AudioFileWriter::CreateRecordingFile( | 269 void AudioDebugFileWriter::AudioFileWriter::CreateRecordingFile( |
240 const base::FilePath& file_name) { | 270 const base::FilePath& file_name) { |
241 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 271 DCHECK(task_runner_->BelongsToCurrentThread()); |
242 DCHECK(!file_.IsValid()); | 272 DCHECK(!file_.IsValid()); |
243 | 273 |
244 file_ = base::File(file_name, | 274 file_ = base::File(file_name, |
245 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 275 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
246 | 276 |
247 if (file_.IsValid()) { | 277 if (file_.IsValid()) { |
248 WriteHeader(); | 278 WriteHeader(); |
249 return; | 279 return; |
250 } | 280 } |
251 | 281 |
252 // Note that we do not inform AudioDebugFileWriter that the file creation | 282 // Note that we do not inform AudioDebugFileWriter that the file creation |
253 // fails, so it will continue to post data to be recorded, which won't | 283 // fails, so it will continue to post data to be recorded, which won't |
254 // be written to the file. This also won't be reflected in WillWrite(). It's | 284 // be written to the file. This also won't be reflected in WillWrite(). It's |
255 // fine, because this situation is rare, and all the posting is expected to | 285 // fine, because this situation is rare, and all the posting is expected to |
256 // happen in case of success anyways. This allows us to save on thread hops | 286 // happen in case of success anyways. This allows us to save on thread hops |
257 // for error reporting and to avoid dealing with lifetime issues. It also | 287 // for error reporting and to avoid dealing with lifetime issues. It also |
258 // means file_.IsValid() should always be checked before issuing writes to it. | 288 // means file_.IsValid() should always be checked before issuing writes to it. |
259 PLOG(ERROR) << "Could not open debug recording file, error=" | 289 PLOG(ERROR) << "Could not open debug recording file, error=" |
260 << file_.error_details(); | 290 << file_.error_details(); |
261 } | 291 } |
262 | 292 |
263 AudioDebugFileWriter::AudioDebugFileWriter( | 293 AudioDebugFileWriter::AudioDebugFileWriter( |
264 const media::AudioParameters& params) | 294 const AudioParameters& params, |
265 : params_(params) { | 295 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
296 : params_(params), file_task_runner_(std::move(file_task_runner)) { | |
266 client_sequence_checker_.DetachFromSequence(); | 297 client_sequence_checker_.DetachFromSequence(); |
267 } | 298 } |
268 | 299 |
269 AudioDebugFileWriter::~AudioDebugFileWriter() { | 300 AudioDebugFileWriter::~AudioDebugFileWriter() { |
270 // |file_writer_| will be deleted on FILE thread. | 301 // |file_writer_| will be deleted on |task_runner_|. |
271 } | 302 } |
272 | 303 |
273 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { | 304 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { |
274 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 305 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
275 DCHECK(!file_writer_); | 306 DCHECK(!file_writer_); |
276 file_writer_ = AudioFileWriter::Create(file_name, params_); | 307 file_writer_ = AudioFileWriter::Create(file_name, params_, file_task_runner_); |
277 } | 308 } |
278 | 309 |
279 void AudioDebugFileWriter::Stop() { | 310 void AudioDebugFileWriter::Stop() { |
280 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 311 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
281 // |file_writer_| is deleted on FILE thread. | 312 // |file_writer_| is deleted on FILE thread. |
282 file_writer_.reset(); | 313 file_writer_.reset(); |
283 client_sequence_checker_.DetachFromSequence(); | 314 client_sequence_checker_.DetachFromSequence(); |
284 } | 315 } |
285 | 316 |
286 void AudioDebugFileWriter::Write(std::unique_ptr<media::AudioBus> data) { | 317 void AudioDebugFileWriter::Write(std::unique_ptr<AudioBus> data) { |
287 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 318 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
288 if (!file_writer_) | 319 if (!file_writer_) |
289 return; | 320 return; |
290 | 321 |
291 // base::Unretained for |file_writer_| is safe, see the destructor. | 322 // base::Unretained for |file_writer_| is safe, see the destructor. |
292 BrowserThread::PostTask( | 323 file_task_runner_->PostTask( |
293 BrowserThread::FILE, FROM_HERE, | 324 FROM_HERE, |
294 // Callback takes ownership of |data|: | 325 // Callback takes ownership of |data|: |
295 base::Bind(&AudioFileWriter::Write, base::Unretained(file_writer_.get()), | 326 base::Bind(&AudioFileWriter::Write, base::Unretained(file_writer_.get()), |
296 base::Owned(data.release()))); | 327 base::Owned(data.release()))); |
297 } | 328 } |
298 | 329 |
299 bool AudioDebugFileWriter::WillWrite() { | 330 bool AudioDebugFileWriter::WillWrite() { |
300 // Note that if this is called from any place other than | 331 // Note that if this is called from any place other than |
301 // |client_sequence_checker_| then there is a data race here, but it's fine, | 332 // |client_sequence_checker_| then there is a data race here, but it's fine, |
302 // because Write() will check for |file_writer_|. So, we are not very precise | 333 // because Write() will check for |file_writer_|. So, we are not very precise |
303 // here, but it's fine: we can afford missing some data or scheduling some | 334 // here, but it's fine: we can afford missing some data or scheduling some |
304 // no-op writes. | 335 // no-op writes. |
305 return !!file_writer_; | 336 return !!file_writer_; |
306 } | 337 } |
307 | 338 |
308 } // namspace content | 339 } // namspace media |
OLD | NEW |