| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/renderer_host/media/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/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "media/base/audio_bus.h" | 15 #include "media/base/audio_bus.h" |
| 16 #include "media/base/audio_sample_types.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // Windows WAVE format header | 22 // Windows WAVE format header |
| 22 // Byte order: Little-endian | 23 // Byte order: Little-endian |
| 23 // Offset Length Content | 24 // Offset Length Content |
| 24 // 0 4 "RIFF" | 25 // 0 4 "RIFF" |
| 25 // 4 4 <file length - 8> | 26 // 4 4 <file length - 8> |
| (...skipping 30 matching lines...) Expand all Loading... |
| 56 public: | 57 public: |
| 57 CharBufferWriter(char* buf, int max_size) | 58 CharBufferWriter(char* buf, int max_size) |
| 58 : buf_(buf), max_size_(max_size), size_(0) {} | 59 : buf_(buf), max_size_(max_size), size_(0) {} |
| 59 | 60 |
| 60 void Write(const char* data, int data_size) { | 61 void Write(const char* data, int data_size) { |
| 61 CHECK_LE(size_ + data_size, max_size_); | 62 CHECK_LE(size_ + data_size, max_size_); |
| 62 memcpy(&buf_[size_], data, data_size); | 63 memcpy(&buf_[size_], data, data_size); |
| 63 size_ += data_size; | 64 size_ += data_size; |
| 64 } | 65 } |
| 65 | 66 |
| 66 void Write(const char(&data)[4]) { Write(static_cast<const char*>(data), 4); } | 67 void Write(const char (&data)[4]) { |
| 68 Write(static_cast<const char*>(data), 4); |
| 69 } |
| 67 | 70 |
| 68 void WriteLE16(uint16_t data) { | 71 void WriteLE16(uint16_t data) { |
| 69 uint16_t val = base::ByteSwapToLE16(data); | 72 uint16_t val = base::ByteSwapToLE16(data); |
| 70 Write(reinterpret_cast<const char*>(&val), sizeof(val)); | 73 Write(reinterpret_cast<const char*>(&val), sizeof(val)); |
| 71 } | 74 } |
| 72 | 75 |
| 73 void WriteLE32(uint32_t data) { | 76 void WriteLE32(uint32_t data) { |
| 74 uint32_t val = base::ByteSwapToLE32(data); | 77 uint32_t val = base::ByteSwapToLE32(data); |
| 75 Write(reinterpret_cast<const char*>(&val), sizeof(val)); | 78 Write(reinterpret_cast<const char*>(&val), sizeof(val)); |
| 76 } | 79 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // FILE message loop destruction. | 182 // FILE message loop destruction. |
| 180 BrowserThread::PostTask( | 183 BrowserThread::PostTask( |
| 181 BrowserThread::FILE, FROM_HERE, | 184 BrowserThread::FILE, FROM_HERE, |
| 182 base::Bind(&AudioFileWriter::CreateRecordingFile, | 185 base::Bind(&AudioFileWriter::CreateRecordingFile, |
| 183 base::Unretained(file_writer.get()), file_name)); | 186 base::Unretained(file_writer.get()), file_name)); |
| 184 return file_writer; | 187 return file_writer; |
| 185 } | 188 } |
| 186 | 189 |
| 187 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( | 190 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( |
| 188 const media::AudioParameters& params) | 191 const media::AudioParameters& params) |
| 189 : samples_(0), params_(params), interleaved_data_size_(0) { | 192 : samples_(0), params_(params), interleaved_data_size_(0) {} |
| 190 DCHECK_EQ(params.bits_per_sample(), kBytesPerSample * 8); | |
| 191 } | |
| 192 | 193 |
| 193 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { | 194 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { |
| 194 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 195 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 195 if (file_.IsValid()) | 196 if (file_.IsValid()) |
| 196 WriteHeader(); | 197 WriteHeader(); |
| 197 } | 198 } |
| 198 | 199 |
| 199 void AudioDebugFileWriter::AudioFileWriter::Write( | 200 void AudioDebugFileWriter::AudioFileWriter::Write(const media::AudioBus* data) { |
| 200 const media::AudioBus* data) { | |
| 201 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 201 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 202 if (!file_.IsValid()) | 202 if (!file_.IsValid()) |
| 203 return; | 203 return; |
| 204 | 204 |
| 205 // Convert to 16 bit audio and write to file. | 205 // Convert to 16 bit audio and write to file. |
| 206 int data_size = data->frames() * data->channels(); | 206 int data_size = data->frames() * data->channels(); |
| 207 if (!interleaved_data_ || interleaved_data_size_ < data_size) { | 207 if (!interleaved_data_ || interleaved_data_size_ < data_size) { |
| 208 interleaved_data_.reset(new int16_t[data_size]); | 208 interleaved_data_.reset(new int16_t[data_size]); |
| 209 interleaved_data_size_ = data_size; | 209 interleaved_data_size_ = data_size; |
| 210 } | 210 } |
| 211 samples_ += data_size; | 211 samples_ += data_size; |
| 212 data->ToInterleaved(data->frames(), sizeof(interleaved_data_[0]), | 212 data->ToInterleaved<media::SignedInt16SampleTypeTraits>( |
| 213 interleaved_data_.get()); | 213 data->frames(), interleaved_data_.get()); |
| 214 | 214 |
| 215 #ifndef ARCH_CPU_LITTLE_ENDIAN | 215 #ifndef ARCH_CPU_LITTLE_ENDIAN |
| 216 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), | 216 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), |
| 217 "Only 2 bytes per channel is supported."); | 217 "Only 2 bytes per channel is supported."); |
| 218 for (int i = 0; i < data_size; ++i) | 218 for (int i = 0; i < data_size; ++i) |
| 219 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); | 219 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); |
| 220 #endif | 220 #endif |
| 221 | 221 |
| 222 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), | 222 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), |
| 223 data_size * sizeof(interleaved_data_[0])); | 223 data_size * sizeof(interleaved_data_[0])); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 253 // fails, so it will continue to post data to be recorded, which won't | 253 // 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 | 254 // 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 | 255 // 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 | 256 // 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 | 257 // 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. | 258 // means file_.IsValid() should always be checked before issuing writes to it. |
| 259 PLOG(ERROR) << "Could not open debug recording file, error=" | 259 PLOG(ERROR) << "Could not open debug recording file, error=" |
| 260 << file_.error_details(); | 260 << file_.error_details(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 AudioDebugFileWriter::AudioDebugFileWriter( | 263 AudioDebugFileWriter::AudioDebugFileWriter(const media::AudioParameters& params) |
| 264 const media::AudioParameters& params) | |
| 265 : params_(params) { | 264 : params_(params) { |
| 266 client_sequence_checker_.DetachFromSequence(); | 265 client_sequence_checker_.DetachFromSequence(); |
| 267 } | 266 } |
| 268 | 267 |
| 269 AudioDebugFileWriter::~AudioDebugFileWriter() { | 268 AudioDebugFileWriter::~AudioDebugFileWriter() { |
| 270 // |file_writer_| will be deleted on FILE thread. | 269 // |file_writer_| will be deleted on FILE thread. |
| 271 } | 270 } |
| 272 | 271 |
| 273 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { | 272 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { |
| 274 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 273 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 298 | 297 |
| 299 bool AudioDebugFileWriter::WillWrite() { | 298 bool AudioDebugFileWriter::WillWrite() { |
| 300 // Note that if this is called from any place other than | 299 // 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, | 300 // |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 | 301 // 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 | 302 // here, but it's fine: we can afford missing some data or scheduling some |
| 304 // no-op writes. | 303 // no-op writes. |
| 305 return !!file_writer_; | 304 return !!file_writer_; |
| 306 } | 305 } |
| 307 | 306 |
| 307 // static |
| 308 std::unique_ptr<media::AudioFileWriter> |
| 309 AudioDebugFileWriter::CreateAudioDebugFileWriter( |
| 310 const media::AudioParameters& params) { |
| 311 return base::MakeUnique<AudioDebugFileWriter>(params); |
| 312 } |
| 313 |
| 308 } // namspace content | 314 } // namspace content |
| OLD | NEW |