OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/media/audio_decoder.h" | 5 #include "webkit/media/audio_decoder.h" |
6 | 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <limits.h> |
| 10 #include <sys/mman.h> |
| 11 #include <unistd.h> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/callback.h" |
| 15 #include "base/file_descriptor_posix.h" |
7 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/posix/eintr_wrapper.h" |
| 18 #include "base/shared_memory.h" |
| 19 #include "media/base/audio_bus.h" |
| 20 #include "media/base/limits.h" |
| 21 #include "third_party/WebKit/Source/Platform/chromium/public/WebAudioBus.h" |
8 | 22 |
9 namespace webkit_media { | 23 namespace webkit_media { |
10 | 24 |
| 25 class AudioDecoderIO { |
| 26 public: |
| 27 AudioDecoderIO(const char* data, size_t data_size); |
| 28 ~AudioDecoderIO(); |
| 29 bool ShareEncodedToProcess(base::SharedMemoryHandle* handle); |
| 30 |
| 31 // Returns true if AudioDecoderIO was successfully created. |
| 32 bool IsValid() const; |
| 33 |
| 34 int read_fd() const { return read_fd_; } |
| 35 int write_fd() const { return write_fd_; } |
| 36 |
| 37 private: |
| 38 // Shared memory that will hold the encoded audio data. This is |
| 39 // used by MediaCodec for decoding. |
| 40 base::SharedMemory encoded_shared_memory_; |
| 41 |
| 42 // A pipe used to communicate with MediaCodec. MediaCodec owns |
| 43 // write_fd_ and writes to it. |
| 44 int read_fd_; |
| 45 int write_fd_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(AudioDecoderIO); |
| 48 }; |
| 49 |
| 50 AudioDecoderIO::AudioDecoderIO(const char* data, size_t data_size) |
| 51 : read_fd_(-1), |
| 52 write_fd_(-1) { |
| 53 |
| 54 if (!data || !data_size || data_size > 0x80000000) |
| 55 return; |
| 56 |
| 57 // Create the shared memory and copy our data to it so that |
| 58 // MediaCodec can access it. |
| 59 encoded_shared_memory_.CreateAndMapAnonymous(data_size); |
| 60 |
| 61 if (!encoded_shared_memory_.memory()) |
| 62 return; |
| 63 |
| 64 memcpy(encoded_shared_memory_.memory(), data, data_size); |
| 65 |
| 66 // Create a pipe for reading/writing the decoded PCM data |
| 67 int pipefd[2]; |
| 68 |
| 69 if (pipe(pipefd)) |
| 70 return; |
| 71 |
| 72 read_fd_ = pipefd[0]; |
| 73 write_fd_ = pipefd[1]; |
| 74 } |
| 75 |
| 76 AudioDecoderIO::~AudioDecoderIO() { |
| 77 // Close the read end of the pipe. The write end should have been |
| 78 // closed by MediaCodec. |
| 79 if (read_fd_ >= 0 && close(read_fd_)) { |
| 80 DVLOG(1) << "Cannot close read fd " << read_fd_ |
| 81 << ": " << strerror(errno); |
| 82 } |
| 83 } |
| 84 |
| 85 bool AudioDecoderIO::IsValid() const { |
| 86 return read_fd_ >= 0 && write_fd_ >= 0 && |
| 87 encoded_shared_memory_.memory(); |
| 88 } |
| 89 |
| 90 bool AudioDecoderIO::ShareEncodedToProcess(base::SharedMemoryHandle* handle) { |
| 91 return encoded_shared_memory_.ShareToProcess( |
| 92 base::Process::Current().handle(), |
| 93 handle); |
| 94 } |
| 95 |
| 96 // To decode audio data, we want to use the Android MediaCodec class. |
| 97 // But this can't run in a sandboxed process so we need initiate the |
| 98 // request to MediaCodec in the browser. To do this, we create a |
| 99 // shared memory buffer that holds the audio data. We send a message |
| 100 // to the browser to start the decoder using this buffer and one end |
| 101 // of a pipe. The MediaCodec class will decode the data from the |
| 102 // shared memory and write the PCM samples back to us over a pipe. |
11 bool DecodeAudioFileData(WebKit::WebAudioBus* destination_bus, const char* data, | 103 bool DecodeAudioFileData(WebKit::WebAudioBus* destination_bus, const char* data, |
12 size_t data_size, double sample_rate) { | 104 size_t data_size, double sample_rate, |
13 NOTIMPLEMENTED(); | 105 const WebAudioMediaCodecRunner& runner) { |
14 return false; | 106 AudioDecoderIO audio_decoder(data, data_size); |
| 107 |
| 108 if (!audio_decoder.IsValid()) |
| 109 return false; |
| 110 |
| 111 base::SharedMemoryHandle encoded_data_handle; |
| 112 audio_decoder.ShareEncodedToProcess(&encoded_data_handle); |
| 113 base::FileDescriptor fd(audio_decoder.write_fd(), true); |
| 114 |
| 115 DVLOG(1) << "DecodeAudioFileData: Starting MediaCodec"; |
| 116 |
| 117 // Start MediaCodec processing in the browser which will read from |
| 118 // encoded_data_handle for our shared memory and write the decoded |
| 119 // PCM samples (16-bit integer) to our pipe. |
| 120 |
| 121 runner.Run(encoded_data_handle, fd); |
| 122 |
| 123 // First, read the number of channels, the sample rate, and the |
| 124 // number of frames and a flag indicating if the file is an |
| 125 // ogg/vorbis file. This must be coordinated with |
| 126 // WebAudioMediaCodecBridge! |
| 127 // |
| 128 // TODO(rtoy): If we know the number of samples, we can create the |
| 129 // destination bus directly and do the conversion directly to the |
| 130 // bus instead of buffering up everything before saving the data to |
| 131 // the bus. |
| 132 |
| 133 int input_fd = audio_decoder.read_fd(); |
| 134 unsigned long info[4]; |
| 135 |
| 136 DVLOG(1) << "Reading audio file info from fd " << input_fd; |
| 137 ssize_t nread = HANDLE_EINTR(read(input_fd, info, sizeof(info))); |
| 138 DVLOG(1) << "read: " << nread << " bytes:\n" |
| 139 << " 0: number of channels = " << info[0] << "\n" |
| 140 << " 1: sample rate = " << info[1] << "\n" |
| 141 << " 2: number of frames = " << info[2] << "\n" |
| 142 << " 3: is vorbis = " << info[3]; |
| 143 |
| 144 if (nread != sizeof(info)) |
| 145 return false; |
| 146 |
| 147 unsigned number_of_channels = info[0]; |
| 148 double file_sample_rate = static_cast<double>(info[1]); |
| 149 |
| 150 // Sanity checks |
| 151 if (!number_of_channels || |
| 152 number_of_channels > media::limits::kMaxChannels || |
| 153 file_sample_rate < media::limits::kMinSampleRate || |
| 154 file_sample_rate > media::limits::kMaxSampleRate) { |
| 155 return false; |
| 156 } |
| 157 |
| 158 int16_t pipe_data[PIPE_BUF / sizeof(int16_t)]; |
| 159 std::vector<int16_t> decoded_samples; |
| 160 |
| 161 // Keep reading from the pipe until it's closed. |
| 162 while ((nread = |
| 163 HANDLE_EINTR(read(input_fd, pipe_data, sizeof(pipe_data)))) > 0) { |
| 164 size_t nsamples = nread / sizeof(int16_t); |
| 165 decoded_samples.reserve(decoded_samples.size() + nsamples); |
| 166 for (size_t k = 0; k < nsamples; ++k) { |
| 167 decoded_samples.push_back(pipe_data[k]); |
| 168 } |
| 169 } |
| 170 |
| 171 DVLOG(1) << "Total samples read = " << decoded_samples.size(); |
| 172 |
| 173 // Convert the samples and save them in the audio bus. |
| 174 size_t number_of_samples = decoded_samples.size(); |
| 175 size_t number_of_frames = number_of_samples / number_of_channels; |
| 176 |
| 177 destination_bus->initialize(number_of_channels, |
| 178 number_of_frames, |
| 179 file_sample_rate); |
| 180 |
| 181 size_t decoded_frames = 0; |
| 182 const float kMaxScale = 1.0f / std::numeric_limits<int16_t>::max(); |
| 183 const float kMinScale = 1.0f / std::numeric_limits<int16_t>::min(); |
| 184 |
| 185 for (size_t m = 0; m < number_of_samples; m += number_of_channels) { |
| 186 for (size_t k = 0; k < number_of_channels; ++k) { |
| 187 int16_t sample = decoded_samples[m + k]; |
| 188 destination_bus->channelData(k)[decoded_frames] = |
| 189 sample * (sample < 0 ? kMinScale : kMaxScale); |
| 190 } |
| 191 ++decoded_frames; |
| 192 } |
| 193 |
| 194 return true; |
15 } | 195 } |
16 | 196 |
17 } // namespace webkit_media | 197 } // namespace webkit_media |
OLD | NEW |