| 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 | |
| 134 // thread. All the operations must be executed on FILE thread. Must be destroyed | |
| 135 // on FILE thread. | |
| 136 class AudioDebugFileWriter::AudioFileWriter { | |
| 137 public: | |
| 138 static AudioFileWriterUniquePtr Create(const base::FilePath& file_name, | |
| 139 const media::AudioParameters& params); | |
| 140 | |
| 141 ~AudioFileWriter(); | |
| 142 | |
| 143 // Write data from |data| to file. | |
| 144 void Write(const media::AudioBus* data); | |
| 145 | |
| 146 private: | |
| 147 AudioFileWriter(const media::AudioParameters& params); | |
| 148 | |
| 149 // Write wave header to file. Called on the FILE thread twice: on construction | |
| 150 // 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 | |
| 152 // actual size info accumulated throughout the object lifetime. | |
| 153 void WriteHeader(); | |
| 154 | |
| 155 void CreateRecordingFile(const base::FilePath& file_name); | |
| 156 | |
| 157 // The file to write to. | |
| 158 base::File file_; | |
| 159 | |
| 160 // Number of written samples. | |
| 161 uint64_t samples_; | |
| 162 | |
| 163 // Input audio parameters required to build wave header. | |
| 164 const media::AudioParameters params_; | |
| 165 | |
| 166 // Intermediate buffer to be written to file. Interleaved 16 bit audio data. | |
| 167 std::unique_ptr<int16_t[]> interleaved_data_; | |
| 168 int interleaved_data_size_; | |
| 169 }; | |
| 170 | |
| 171 // static | 135 // static |
| 172 AudioDebugFileWriter::AudioFileWriterUniquePtr | 136 AudioDebugFileWriter::AudioFileWriterUniquePtr |
| 173 AudioDebugFileWriter::AudioFileWriter::Create( | 137 AudioDebugFileWriter::AudioFileWriter::Create( |
| 174 const base::FilePath& file_name, | 138 const base::FilePath& file_name, |
| 175 const media::AudioParameters& params) { | 139 const media::AudioParameters& params, |
| 176 AudioFileWriterUniquePtr file_writer(new AudioFileWriter(params)); | 140 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
| 141 AudioFileWriterUniquePtr file_writer( |
| 142 new AudioFileWriter(params, file_task_runner)); |
| 177 | 143 |
| 178 // base::Unretained is safe, because destructor is called on FILE thread or on | 144 // base::Unretained is safe, because destructor is called on |
| 179 // FILE message loop destruction. | 145 // |file_task_runner|. |
| 180 BrowserThread::PostTask( | 146 file_task_runner->PostTask( |
| 181 BrowserThread::FILE, FROM_HERE, | 147 FROM_HERE, base::Bind(&AudioFileWriter::CreateRecordingFile, |
| 182 base::Bind(&AudioFileWriter::CreateRecordingFile, | 148 base::Unretained(file_writer.get()), file_name)); |
| 183 base::Unretained(file_writer.get()), file_name)); | |
| 184 return file_writer; | 149 return file_writer; |
| 185 } | 150 } |
| 186 | 151 |
| 187 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( | 152 AudioDebugFileWriter::AudioFileWriter::AudioFileWriter( |
| 188 const media::AudioParameters& params) | 153 const media::AudioParameters& params, |
| 189 : samples_(0), params_(params), interleaved_data_size_(0) { | 154 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 155 : samples_(0), |
| 156 params_(params), |
| 157 interleaved_data_size_(0), |
| 158 file_task_runner_(std::move(file_task_runner)) { |
| 190 DCHECK_EQ(params.bits_per_sample(), kBytesPerSample * 8); | 159 DCHECK_EQ(params.bits_per_sample(), kBytesPerSample * 8); |
| 191 } | 160 } |
| 192 | 161 |
| 193 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { | 162 AudioDebugFileWriter::AudioFileWriter::~AudioFileWriter() { |
| 194 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 163 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
| 195 if (file_.IsValid()) | 164 if (file_.IsValid()) |
| 196 WriteHeader(); | 165 WriteHeader(); |
| 197 } | 166 } |
| 198 | 167 |
| 199 void AudioDebugFileWriter::AudioFileWriter::Write( | 168 void AudioDebugFileWriter::AudioFileWriter::Write(const media::AudioBus* data) { |
| 200 const media::AudioBus* data) { | 169 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
| 201 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 202 if (!file_.IsValid()) | 170 if (!file_.IsValid()) |
| 203 return; | 171 return; |
| 204 | 172 |
| 205 // Convert to 16 bit audio and write to file. | 173 // Convert to 16 bit audio and write to file. |
| 206 int data_size = data->frames() * data->channels(); | 174 int data_size = data->frames() * data->channels(); |
| 207 if (!interleaved_data_ || interleaved_data_size_ < data_size) { | 175 if (!interleaved_data_ || interleaved_data_size_ < data_size) { |
| 208 interleaved_data_.reset(new int16_t[data_size]); | 176 interleaved_data_.reset(new int16_t[data_size]); |
| 209 interleaved_data_size_ = data_size; | 177 interleaved_data_size_ = data_size; |
| 210 } | 178 } |
| 211 samples_ += data_size; | 179 samples_ += data_size; |
| 212 data->ToInterleaved(data->frames(), sizeof(interleaved_data_[0]), | 180 data->ToInterleaved(data->frames(), sizeof(interleaved_data_[0]), |
| 213 interleaved_data_.get()); | 181 interleaved_data_.get()); |
| 214 | 182 |
| 215 #ifndef ARCH_CPU_LITTLE_ENDIAN | 183 #ifndef ARCH_CPU_LITTLE_ENDIAN |
| 216 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), | 184 static_assert(sizeof(interleaved_data_[0]) == sizeof(uint16_t), |
| 217 "Only 2 bytes per channel is supported."); | 185 "Only 2 bytes per channel is supported."); |
| 218 for (int i = 0; i < data_size; ++i) | 186 for (int i = 0; i < data_size; ++i) |
| 219 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); | 187 interleaved_data_[i] = base::ByteSwapToLE16(interleaved_data_[i]); |
| 220 #endif | 188 #endif |
| 221 | 189 |
| 222 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), | 190 file_.WriteAtCurrentPos(reinterpret_cast<char*>(interleaved_data_.get()), |
| 223 data_size * sizeof(interleaved_data_[0])); | 191 data_size * sizeof(interleaved_data_[0])); |
| 224 } | 192 } |
| 225 | 193 |
| 226 void AudioDebugFileWriter::AudioFileWriter::WriteHeader() { | 194 void AudioDebugFileWriter::AudioFileWriter::WriteHeader() { |
| 227 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 195 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
| 228 if (!file_.IsValid()) | 196 if (!file_.IsValid()) |
| 229 return; | 197 return; |
| 230 WavHeaderBuffer buf; | 198 WavHeaderBuffer buf; |
| 231 WriteWavHeader(&buf, params_.channels(), params_.sample_rate(), samples_); | 199 WriteWavHeader(&buf, params_.channels(), params_.sample_rate(), samples_); |
| 232 file_.Write(0, &buf[0], kWavHeaderSize); | 200 file_.Write(0, &buf[0], kWavHeaderSize); |
| 233 | 201 |
| 234 // Write() does not move the cursor if file is not in APPEND mode; Seek() so | 202 // 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. | 203 // that the header is not overwritten by the following writes. |
| 236 file_.Seek(base::File::FROM_BEGIN, kWavHeaderSize); | 204 file_.Seek(base::File::FROM_BEGIN, kWavHeaderSize); |
| 237 } | 205 } |
| 238 | 206 |
| 239 void AudioDebugFileWriter::AudioFileWriter::CreateRecordingFile( | 207 void AudioDebugFileWriter::AudioFileWriter::CreateRecordingFile( |
| 240 const base::FilePath& file_name) { | 208 const base::FilePath& file_name) { |
| 241 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 209 DCHECK(file_task_runner_->BelongsToCurrentThread()); |
| 242 DCHECK(!file_.IsValid()); | 210 DCHECK(!file_.IsValid()); |
| 243 | 211 |
| 244 file_ = base::File(file_name, | 212 file_ = base::File(file_name, |
| 245 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 213 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 246 | 214 |
| 247 if (file_.IsValid()) { | 215 if (file_.IsValid()) { |
| 248 WriteHeader(); | 216 WriteHeader(); |
| 249 return; | 217 return; |
| 250 } | 218 } |
| 251 | 219 |
| 252 // Note that we do not inform AudioDebugFileWriter that the file creation | 220 // 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 | 221 // 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 | 222 // 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 | 223 // 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 | 224 // 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 | 225 // 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. | 226 // means file_.IsValid() should always be checked before issuing writes to it. |
| 259 PLOG(ERROR) << "Could not open debug recording file, error=" | 227 PLOG(ERROR) << "Could not open debug recording file, error=" |
| 260 << file_.error_details(); | 228 << file_.error_details(); |
| 261 } | 229 } |
| 262 | 230 |
| 263 AudioDebugFileWriter::AudioDebugFileWriter( | 231 AudioDebugFileWriter::AudioDebugFileWriter( |
| 264 const media::AudioParameters& params) | 232 const media::AudioParameters& params, |
| 265 : params_(params) { | 233 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 234 : params_(params), file_task_runner_(std::move(file_task_runner)) { |
| 266 client_sequence_checker_.DetachFromSequence(); | 235 client_sequence_checker_.DetachFromSequence(); |
| 267 } | 236 } |
| 268 | 237 |
| 269 AudioDebugFileWriter::~AudioDebugFileWriter() { | 238 AudioDebugFileWriter::~AudioDebugFileWriter() { |
| 270 // |file_writer_| will be deleted on FILE thread. | 239 // |file_writer_| will be deleted on |file_task_runner_|. |
| 271 } | 240 } |
| 272 | 241 |
| 273 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { | 242 void AudioDebugFileWriter::Start(const base::FilePath& file_name) { |
| 274 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 243 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
| 275 DCHECK(!file_writer_); | 244 DCHECK(!file_writer_); |
| 276 file_writer_ = AudioFileWriter::Create(file_name, params_); | 245 file_writer_ = AudioFileWriter::Create(file_name, params_, file_task_runner_); |
| 277 } | 246 } |
| 278 | 247 |
| 279 void AudioDebugFileWriter::Stop() { | 248 void AudioDebugFileWriter::Stop() { |
| 280 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 249 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
| 281 // |file_writer_| is deleted on FILE thread. | 250 // |file_writer_| is deleted on FILE thread. |
| 282 file_writer_.reset(); | 251 file_writer_.reset(); |
| 283 client_sequence_checker_.DetachFromSequence(); | 252 client_sequence_checker_.DetachFromSequence(); |
| 284 } | 253 } |
| 285 | 254 |
| 286 void AudioDebugFileWriter::Write(std::unique_ptr<media::AudioBus> data) { | 255 void AudioDebugFileWriter::Write(std::unique_ptr<media::AudioBus> data) { |
| 287 DCHECK(client_sequence_checker_.CalledOnValidSequence()); | 256 DCHECK(client_sequence_checker_.CalledOnValidSequence()); |
| 288 if (!file_writer_) | 257 if (!file_writer_) |
| 289 return; | 258 return; |
| 290 | 259 |
| 291 // base::Unretained for |file_writer_| is safe, see the destructor. | 260 // base::Unretained for |file_writer_| is safe, see the destructor. |
| 292 BrowserThread::PostTask( | 261 file_task_runner_->PostTask( |
| 293 BrowserThread::FILE, FROM_HERE, | 262 FROM_HERE, |
| 294 // Callback takes ownership of |data|: | 263 // Callback takes ownership of |data|: |
| 295 base::Bind(&AudioFileWriter::Write, base::Unretained(file_writer_.get()), | 264 base::Bind(&AudioFileWriter::Write, base::Unretained(file_writer_.get()), |
| 296 base::Owned(data.release()))); | 265 base::Owned(data.release()))); |
| 297 } | 266 } |
| 298 | 267 |
| 299 bool AudioDebugFileWriter::WillWrite() { | 268 bool AudioDebugFileWriter::WillWrite() { |
| 300 // Note that if this is called from any place other than | 269 // 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, | 270 // |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 | 271 // 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 | 272 // here, but it's fine: we can afford missing some data or scheduling some |
| 304 // no-op writes. | 273 // no-op writes. |
| 305 return !!file_writer_; | 274 return !!file_writer_; |
| 306 } | 275 } |
| 307 | 276 |
| 308 } // namspace content | 277 } // namspace media |
| OLD | NEW |