[MemSheriff] More sendto parameter issues.
[chromium-blink-merge.git] / media / audio / sounds / wav_audio_handler.cc
blobb87baa8fd3f4995bce187d9303861c320ec7ca5b
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
3 // found in the LICENSE file.
5 #include "media/audio/sounds/wav_audio_handler.h"
7 #include <algorithm>
8 #include <cstring>
10 #include "base/logging.h"
11 #include "base/sys_byteorder.h"
12 #include "media/base/audio_bus.h"
14 namespace {
16 const char kChunkId[] = "RIFF";
17 const char kFormat[] = "WAVE";
18 const char kSubchunk1Id[] = "fmt ";
19 const char kSubchunk2Id[] = "data";
21 // The size of the header of a wav file. The header consists of 'RIFF', 4 bytes
22 // of total data length, and 'WAVE'.
23 const size_t kWavFileHeaderSize = 12;
25 // The size of a chunk header in wav file format. A chunk header consists of a
26 // tag ('fmt ' or 'data') and 4 bytes of chunk length.
27 const size_t kChunkHeaderSize = 8;
29 // The minimum size of 'fmt' chunk.
30 const size_t kFmtChunkMinimumSize = 16;
32 // The offsets of 'fmt' fields.
33 const size_t kAudioFormatOffset = 0;
34 const size_t kChannelOffset = 2;
35 const size_t kSampleRateOffset = 4;
36 const size_t kBitsPerSampleOffset = 14;
38 // Some constants for audio format.
39 const int kAudioFormatPCM = 1;
41 // Reads an integer from |data| with |offset|.
42 template <typename T>
43 T ReadInt(const base::StringPiece& data, size_t offset) {
44 CHECK_LE(offset + sizeof(T), data.size());
45 T result;
46 memcpy(&result, data.data() + offset, sizeof(T));
47 #if !defined(ARCH_CPU_LITTLE_ENDIAN)
48 result = base::ByteSwap(result);
49 #endif
50 return result;
53 } // namespace
55 namespace media {
57 WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data)
58 : num_channels_(0),
59 sample_rate_(0),
60 bits_per_sample_(0) {
61 CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small";
62 CHECK(wav_data.starts_with(kChunkId) &&
63 memcmp(wav_data.data() + 8, kFormat, 4) == 0)
64 << "incorrect wav header";
66 uint32 total_length = std::min(ReadInt<uint32>(wav_data, 4),
67 static_cast<uint32>(wav_data.size()));
68 uint32 offset = kWavFileHeaderSize;
69 while (offset < total_length) {
70 const int length = ParseSubChunk(wav_data.substr(offset));
71 CHECK_LE(0, length) << "can't parse wav sub-chunk";
72 offset += length;
75 const int frame_count = data_.size() * 8 / num_channels_ / bits_per_sample_;
76 params_ = AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
77 GuessChannelLayout(num_channels_),
78 sample_rate_,
79 bits_per_sample_,
80 frame_count);
83 WavAudioHandler::~WavAudioHandler() {}
85 bool WavAudioHandler::AtEnd(size_t cursor) const {
86 return data_.size() <= cursor;
89 bool WavAudioHandler::CopyTo(AudioBus* bus,
90 size_t cursor,
91 size_t* bytes_written) const {
92 if (!bus)
93 return false;
94 if (bus->channels() != params_.channels()) {
95 DVLOG(1) << "Number of channel mismatch.";
96 return false;
98 if (AtEnd(cursor)) {
99 bus->Zero();
100 return true;
102 const int remaining_frames =
103 (data_.size() - cursor) / params_.GetBytesPerFrame();
104 const int frames = std::min(bus->frames(), remaining_frames);
105 bus->FromInterleaved(data_.data() + cursor, frames,
106 params_.bits_per_sample() / 8);
107 *bytes_written = frames * params_.GetBytesPerFrame();
108 bus->ZeroFramesPartial(frames, bus->frames() - frames);
109 return true;
112 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) {
113 if (data.size() < kChunkHeaderSize)
114 return data.size();
115 uint32 chunk_length = ReadInt<uint32>(data, 4);
116 if (data.starts_with(kSubchunk1Id)) {
117 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length)))
118 return -1;
119 } else if (data.starts_with(kSubchunk2Id)) {
120 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length)))
121 return -1;
122 } else {
123 DVLOG(1) << "Unknown data chunk: " << data.substr(0, 4) << ".";
125 return chunk_length + kChunkHeaderSize;
128 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) {
129 if (data.size() < kFmtChunkMinimumSize) {
130 DLOG(ERROR) << "Data size " << data.size() << " is too short.";
131 return false;
133 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM);
134 num_channels_ = ReadInt<uint16>(data, kChannelOffset);
135 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset);
136 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset);
137 return true;
140 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) {
141 data_ = data;
142 return true;
145 } // namespace media