| 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 void WriteHeader(); | 156 void WriteHeader(); |
| 154 | 157 |
| 155 void CreateRecordingFile(const base::FilePath& file_name); | 158 void CreateRecordingFile(const base::FilePath& file_name); |
| 156 | 159 |
| 157 // The file to write to. | 160 // The file to write to. |
| 158 base::File file_; | 161 base::File file_; |
| 159 | 162 |
| 160 // Number of written samples. | 163 // Number of written samples. |
| 161 uint64_t samples_; | 164 uint64_t samples_; |
| 162 | 165 |
| 163 // Input audio parameters required to build wave header. | 166 // Audio parameters required to build wave header. Number of channels and |
| 167 // sample rate are used. |
| 164 const media::AudioParameters params_; | 168 const media::AudioParameters params_; |
| 165 | 169 |
| 166 // Intermediate buffer to be written to file. Interleaved 16 bit audio data. | 170 // Intermediate buffer to be written to file. Interleaved 16 bit audio data. |
| 167 std::unique_ptr<int16_t[]> interleaved_data_; | 171 std::unique_ptr<int16_t[]> interleaved_data_; |
| 168 int interleaved_data_size_; | 172 int interleaved_data_size_; |
| 169 }; | 173 }; |
| 170 | 174 |
| 171 // static | 175 // static |
| 172 AudioDebugFileWriter::AudioFileWriterUniquePtr | 176 AudioDebugFileWriter::AudioFileWriterUniquePtr |
| 173 AudioDebugFileWriter::AudioFileWriter::Create( | 177 AudioDebugFileWriter::AudioFileWriter::Create( |
| 174 const base::FilePath& file_name, | 178 const base::FilePath& file_name, |
| 175 const media::AudioParameters& params) { | 179 const media::AudioParameters& params) { |
| 176 AudioFileWriterUniquePtr file_writer(new AudioFileWriter(params)); | 180 AudioFileWriterUniquePtr file_writer(new AudioFileWriter(params)); |
| 177 | 181 |
| 178 // base::Unretained is safe, because destructor is called on FILE thread or on | 182 // base::Unretained is safe, because destructor is called on FILE thread or on |
| 179 // FILE message loop destruction. | 183 // FILE message loop destruction. |
| 180 BrowserThread::PostTask( | 184 BrowserThread::PostTask( |
| 181 BrowserThread::FILE, FROM_HERE, | 185 BrowserThread::FILE, FROM_HERE, |
| 182 base::Bind(&AudioFileWriter::CreateRecordingFile, | 186 base::Bind(&AudioFileWriter::CreateRecordingFile, |
| 183 base::Unretained(file_writer.get()), file_name)); | 187 base::Unretained(file_writer.get()), file_name)); |
| 184 return file_writer; | 188 return file_writer; |
| 185 } | 189 } |
| 186 | 190 |
| 187 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( | 191 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( |
| 188 const media::AudioParameters& params) | 192 const media::AudioParameters& params) |
| 189 : samples_(0), params_(params), interleaved_data_size_(0) { | 193 : samples_(0), params_(params), interleaved_data_size_(0) {} |
| 190 DCHECK_EQ(params.bits_per_sample(), kBytesPerSample * 8); | |
| 191 } | |
| 192 | 194 |
| 193 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { | 195 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { |
| 194 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 196 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 195 if (file_.IsValid()) | 197 if (file_.IsValid()) |
| 196 WriteHeader(); | 198 WriteHeader(); |
| 197 } | 199 } |
| 198 | 200 |
| 199 void AudioDebugFileWriter::AudioFileWriter::Write( | 201 void AudioDebugFileWriter::AudioFileWriter::Write(const media::AudioBus* data) { |
| 200 const media::AudioBus* data) { | |
| 201 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 202 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 203 DCHECK_EQ(params_.channels(), data->channels()); |
| 202 if (!file_.IsValid()) | 204 if (!file_.IsValid()) |
| 203 return; | 205 return; |
| 204 | 206 |
| 205 // Convert to 16 bit audio and write to file. | 207 // Convert to 16 bit audio and write to file. |
| 206 int data_size = data->frames() * data->channels(); | 208 int data_size = data->frames() * data->channels(); |
| 207 if (!interleaved_data_ || interleaved_data_size_ < data_size) { | 209 if (!interleaved_data_ || interleaved_data_size_ < data_size) { |
| 208 interleaved_data_.reset(new int16_t[data_size]); | 210 interleaved_data_.reset(new int16_t[data_size]); |
| 209 interleaved_data_size_ = data_size; | 211 interleaved_data_size_ = data_size; |
| 210 } | 212 } |
| 211 samples_ += data_size; | 213 samples_ += data_size; |
| 212 data->ToInterleaved(data->frames(), sizeof(interleaved_data_[0]), | 214 data->ToInterleaved<media::SignedInt16SampleTypeTraits>( |
| 213 interleaved_data_.get()); | 215 data->frames(), interleaved_data_.get()); |
| 214 | 216 |
| 215 #ifndef ARCH_CPU_LITTLE_ENDIAN | 217 #ifndef ARCH_CPU_LITTLE_ENDIAN |
| 216 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), | 218 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), |
| 217 "Only 2 bytes per channel is supported."); | 219 "Only 2 bytes per channel is supported."); |
| 218 for (int i = 0; i < data_size; ++i) | 220 for (int i = 0; i < data_size; ++i) |
| 219 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); | 221 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); |
| 220 #endif | 222 #endif |
| 221 | 223 |
| 222 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), | 224 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), |
| 223 data_size * sizeof(interleaved_data_[0])); | 225 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 | 255 // 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 | 256 // 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 | 257 // 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 | 258 // 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 | 259 // 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. | 260 // means file_.IsValid() should always be checked before issuing writes to it. |
| 259 PLOG(ERROR) << "Could not open debug recording file, error=" | 261 PLOG(ERROR) << "Could not open debug recording file, error=" |
| 260 << file_.error_details(); | 262 << file_.error_details(); |
| 261 } | 263 } |
| 262 | 264 |
| 263 AudioDebugFileWriter::AudioDebugFileWriter( | 265 AudioDebugFileWriter::AudioDebugFileWriter(const media::AudioParameters& params) |
| 264 const media::AudioParameters& params) | |
| 265 : params_(params) { | 266 : params_(params) { |
| 266 client_sequence_checker_.DetachFromSequence(); | 267 client_sequence_checker_.DetachFromSequence(); |
| 267 } | 268 } |
| 268 | 269 |
| 269 AudioDebugFileWriter::~AudioDebugFileWriter() { | 270 AudioDebugFileWriter::~AudioDebugFileWriter() { |
| 270 // |file_writer_| will be deleted on FILE thread. | 271 // |file_writer_| will be deleted on FILE thread. |
| 271 } | 272 } |
| 272 | 273 |
| 274 // static |
| 275 std::unique_ptr<media::AudioFileWriter> AudioDebugFileWriter::Create( |
| 276 const media::AudioParameters& params) { |
| 277 return base::MakeUnique<AudioDebugFileWriter>(params); |
| 278 } |
| 279 |
| 273 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { | 280 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { |
| 274 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 281 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
| 275 DCHECK(!file_writer_); | 282 DCHECK(!file_writer_); |
| 276 file_writer_ = AudioFileWriter::Create(file_name, params_); | 283 file_writer_ = AudioFileWriter::Create(file_name, params_); |
| 277 } | 284 } |
| 278 | 285 |
| 279 void AudioDebugFileWriter::Stop() { | 286 void AudioDebugFileWriter::Stop() { |
| 280 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 287 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
| 281 // |file_writer_| is deleted on FILE thread. | 288 // |file_writer_| is deleted on FILE thread. |
| 282 file_writer_.reset(); | 289 file_writer_.reset(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 298 | 305 |
| 299 bool AudioDebugFileWriter::WillWrite() { | 306 bool AudioDebugFileWriter::WillWrite() { |
| 300 // Note that if this is called from any place other than | 307 // 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, | 308 // |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 | 309 // 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 | 310 // here, but it's fine: we can afford missing some data or scheduling some |
| 304 // no-op writes. | 311 // no-op writes. |
| 305 return !!file_writer_; | 312 return !!file_writer_; |
| 306 } | 313 } |
| 307 | 314 |
| 315 const base::FilePath::CharType* AudioDebugFileWriter::GetFileNameExtension() { |
| 316 return FILE_PATH_LITERAL("wav"); |
| 317 } |
| 318 |
| 308 } // namspace content | 319 } // namspace content |
| OLD | NEW |