Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "media/audio/sounds/wav_audio_handler.h" | 5 #include "media/audio/sounds/wav_audio_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 const size_t kAudioFormatOffset = 0; | 33 const size_t kAudioFormatOffset = 0; |
| 34 const size_t kChannelOffset = 2; | 34 const size_t kChannelOffset = 2; |
| 35 const size_t kSampleRateOffset = 4; | 35 const size_t kSampleRateOffset = 4; |
| 36 const size_t kByteRateOffset = 8; | 36 const size_t kByteRateOffset = 8; |
| 37 const size_t kBitsPerSampleOffset = 14; | 37 const size_t kBitsPerSampleOffset = 14; |
| 38 | 38 |
| 39 // Some constants for audio format. | 39 // Some constants for audio format. |
| 40 const int kAudioFormatPCM = 1; | 40 const int kAudioFormatPCM = 1; |
| 41 | 41 |
| 42 // Reads an integer from |data| with |offset|. | 42 // Reads an integer from |data| with |offset|. |
| 43 template<typename T> T ReadInt(const base::StringPiece& data, size_t offset) { | 43 template <typename T> |
| 44 T ReadInt(const base::StringPiece& data, size_t offset) { | |
| 44 CHECK_LE(offset + sizeof(T), data.size()); | 45 CHECK_LE(offset + sizeof(T), data.size()); |
| 45 T result; | 46 T result; |
| 46 memcpy(&result, data.data() + offset, sizeof(T)); | 47 memcpy(&result, data.data() + offset, sizeof(T)); |
| 47 #if !defined(ARCH_CPU_LITTLE_ENDIAN) | 48 #if !defined(ARCH_CPU_LITTLE_ENDIAN) |
| 48 result = base::ByteSwap(result); | 49 result = base::ByteSwap(result); |
| 49 #endif | 50 #endif |
| 50 return result; | 51 return result; |
| 51 } | 52 } |
| 52 | 53 |
| 53 } // namespace | 54 } // namespace |
| 54 | 55 |
| 55 namespace media { | 56 namespace media { |
| 56 | 57 |
| 57 WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) | 58 WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) |
| 58 : num_channels_(0), | 59 : num_channels_(0), |
| 59 sample_rate_(0), | 60 sample_rate_(0), |
| 60 byte_rate_(0), | |
| 61 bits_per_sample_(0) { | 61 bits_per_sample_(0) { |
| 62 CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small"; | 62 CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small"; |
| 63 CHECK(wav_data.starts_with(kChunkId) && | 63 CHECK(wav_data.starts_with(kChunkId) && |
| 64 memcmp(wav_data.data() + 8, kFormat, 4) == 0) | 64 memcmp(wav_data.data() + 8, kFormat, 4) == 0) |
| 65 << "incorrect wav header"; | 65 << "incorrect wav header"; |
| 66 | 66 |
| 67 uint32 total_length = std::min(ReadInt<uint32>(wav_data, 4), | 67 uint32 total_length = std::min(ReadInt<uint32>(wav_data, 4), |
| 68 static_cast<uint32>(wav_data.size())); | 68 static_cast<uint32>(wav_data.size())); |
| 69 uint32 offset = kWavFileHeaderSize; | 69 uint32 offset = kWavFileHeaderSize; |
| 70 while (offset < total_length) { | 70 while (offset < total_length) { |
| 71 const int length = ParseSubChunk(wav_data.substr(offset)); | 71 const int length = ParseSubChunk(wav_data.substr(offset)); |
| 72 CHECK_LE(0, length) << "can't parse wav sub-chunk"; | 72 CHECK_LE(0, length) << "can't parse wav sub-chunk"; |
| 73 offset += length; | 73 offset += length; |
| 74 } | 74 } |
| 75 | |
| 76 const int frame_count = data_.size() * 8 / num_channels_ / bits_per_sample_; | |
| 77 params_ = AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 78 GuessChannelLayout(num_channels_), | |
| 79 sample_rate_, | |
| 80 bits_per_sample_, | |
| 81 frame_count); | |
| 75 } | 82 } |
| 76 | 83 |
| 77 WavAudioHandler::~WavAudioHandler() { | 84 WavAudioHandler::~WavAudioHandler() {} |
| 78 } | |
| 79 | 85 |
| 80 bool WavAudioHandler::AtEnd(size_t cursor) const { | 86 bool WavAudioHandler::AtEnd(size_t cursor) const { |
| 81 return data_.size() <= cursor; | 87 return data_.size() <= cursor; |
| 82 } | 88 } |
| 83 | 89 |
| 84 bool WavAudioHandler::CopyTo(AudioBus* bus, | 90 bool WavAudioHandler::CopyTo(AudioBus* bus, |
| 85 size_t cursor, | 91 size_t cursor, |
| 86 size_t* bytes_written) const { | 92 size_t* bytes_written) const { |
| 87 if (!bus) | 93 if (!bus) |
| 88 return false; | 94 return false; |
| 89 if (bus->channels() != num_channels_) { | 95 if (bus->channels() != params_.channels()) { |
| 90 DLOG(ERROR) << "Number of channel mismatch."; | 96 VLOG(1) << "Number of channel mismatch."; |
|
DaleCurtis
2014/01/09 19:58:26
DVLOG(1) ?
ygorshenin1
2014/01/13 10:00:55
Done.
| |
| 91 return false; | 97 return false; |
| 92 } | 98 } |
| 93 if (AtEnd(cursor)) { | 99 if (AtEnd(cursor)) { |
| 94 bus->Zero(); | 100 bus->Zero(); |
| 95 return true; | 101 return true; |
| 96 } | 102 } |
| 97 const int remaining_frames = (data_.size() - cursor) / bytes_per_frame_; | 103 const int remaining_frames = |
| 104 (data_.size() - cursor) / params_.GetBytesPerFrame(); | |
| 98 const int frames = std::min(bus->frames(), remaining_frames); | 105 const int frames = std::min(bus->frames(), remaining_frames); |
| 99 bus->FromInterleaved(data_.data() + cursor, frames, bytes_per_sample_); | 106 bus->FromInterleaved(data_.data() + cursor, frames, |
| 100 *bytes_written = frames * bytes_per_frame_; | 107 params_.bits_per_sample() / 8); |
| 108 *bytes_written = frames * params_.GetBytesPerFrame(); | |
| 101 bus->ZeroFramesPartial(frames, bus->frames() - frames); | 109 bus->ZeroFramesPartial(frames, bus->frames() - frames); |
| 102 return true; | 110 return true; |
| 103 } | 111 } |
| 104 | 112 |
| 105 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) { | 113 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) { |
| 106 if (data.size() < kChunkHeaderSize) | 114 if (data.size() < kChunkHeaderSize) |
| 107 return data.size(); | 115 return data.size(); |
| 108 uint32 chunk_length = ReadInt<uint32>(data, 4); | 116 uint32 chunk_length = ReadInt<uint32>(data, 4); |
| 109 if (data.starts_with(kSubchunk1Id)) { | 117 if (data.starts_with(kSubchunk1Id)) { |
| 110 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) | 118 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) |
| 111 return -1; | 119 return -1; |
| 112 } else if (data.starts_with(kSubchunk2Id)) { | 120 } else if (data.starts_with(kSubchunk2Id)) { |
| 113 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) | 121 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) |
| 114 return -1; | 122 return -1; |
| 115 } else { | 123 } else { |
| 116 DVLOG(1) << "Unknown data chunk: " << data.substr(0, 4) << "."; | 124 DVLOG(1) << "Unknown data chunk: " << data.substr(0, 4) << "."; |
| 117 } | 125 } |
| 118 return chunk_length + kChunkHeaderSize; | 126 return chunk_length + kChunkHeaderSize; |
| 119 } | 127 } |
| 120 | 128 |
| 121 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { | 129 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { |
| 122 if (data.size() < kFmtChunkMinimumSize) { | 130 if (data.size() < kFmtChunkMinimumSize) { |
| 123 DLOG(ERROR) << "Data size " << data.size() << " is too short."; | 131 DLOG(ERROR) << "Data size " << data.size() << " is too short."; |
| 124 return false; | 132 return false; |
| 125 } | 133 } |
| 126 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); | 134 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); |
| 127 num_channels_ = ReadInt<uint16>(data, kChannelOffset); | 135 num_channels_ = ReadInt<uint16>(data, kChannelOffset); |
| 128 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); | 136 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); |
| 129 byte_rate_ = ReadInt<uint32>(data, kByteRateOffset); | |
| 130 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); | 137 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); |
| 131 bytes_per_sample_ = bits_per_sample_ >> 3; | |
| 132 bytes_per_frame_ = num_channels_ * bytes_per_sample_; | |
| 133 return true; | 138 return true; |
| 134 } | 139 } |
| 135 | 140 |
| 136 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) { | 141 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) { |
| 137 data_ = data; | 142 data_ = data; |
| 138 return true; | 143 return true; |
| 139 } | 144 } |
| 140 | 145 |
| 141 } // namespace media | 146 } // namespace media |
| OLD | NEW |