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