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