Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(993)

Side by Side Diff: webkit/media/android/audio_decoder_android.cc

Issue 12457043: Android implementation of WebAudio audio file decoder (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 is_valid() { return is_valid_; }
28 int getReadFd() { return pipefd_[0]; };
29 int getWriteFd() { return pipefd_[1]; };
bulach 2013/03/28 13:39:25 nit: add a \n also, please make 28-29 either get_r
Raymond Toy (Google) 2013/03/28 17:38:31 I was following http://google-styleguide.googlecod
bulach 2013/03/28 17:42:31 hmm, I think that means either: GetReadFd() or get
30 private:
31 // Shared memory that will hold the encoded audio data. This is
32 // used by MediaCodec for decoding.
33 base::SharedMemory encoded_shared_memory_;
34 // The MediaCodec process will write the pcm data to this pipe.
35 int pipefd_[2];
36 bool is_valid_;
37 };
38
39 AudioDecoderIO::AudioDecoderIO(const char* data, size_t data_size)
40 : is_valid_(true) {
41 pipefd_[0] = -1;
42 pipefd_[1] = -1;
43
44 // Create the shared memory and copy our data to it so that
45 // MediaCodec can access it.
46 encoded_shared_memory_.CreateAndMapAnonymous(data_size);
47 if (encoded_shared_memory_.memory()) {
48 DVLOG(0) << "Memory mapped to " << encoded_shared_memory_.memory()
49 << " for " << data_size << " bytes";
50 memcpy(encoded_shared_memory_.memory(), data, data_size);
51 } else {
52 is_valid_ = false;
53 }
54
55 // Create a pipe for reading/writing the decoded pcm data
56
57 if (pipe(pipefd_)) {
58 // Pipe was not created
59 pipefd_[0] = -1;
60 pipefd_[1] = -1;
61 is_valid_ = false;
62 }
63
64 DVLOG(0) << "pipefds: read fd = " << pipefd_[0]
65 << " write fd = " << pipefd_[1];
66 }
67
68 AudioDecoderIO::~AudioDecoderIO() {
69 // Close the read end of the pipe. The write end should have been
70 // closed by MediaCodec.
71 if (pipefd_[0] >= 0) {
72 DVLOG(0) << "Closing read end of pipe: " << pipefd_[0];
73 if (close(pipefd_[0]))
74 DVLOG(0) << "Failed to close read end!";
75 }
76 }
77
78 bool AudioDecoderIO::ShareEncodedToProcess(base::SharedMemoryHandle* handle) {
79 return encoded_shared_memory_.ShareToProcess(
80 base::Process::Current().handle(),
81 handle);
82 }
83
84 //
85 // To decode audio data, we want to use the Android MediaCodec class.
86 // But this can't run in a sandboxed process so we need to do the
87 // decoding in the browser. To do this, we create a shared memory
88 // buffer that holds the audio data. We send a message to the browser
89 // to start the decoder using this buffer and one end of a pipe. The
90 // MediaCodec class will decode the data from the shared memory and
91 // write the pcm samples back to us over a pipe.
11 bool DecodeAudioFileData(WebKit::WebAudioBus* destination_bus, const char* data, 92 bool DecodeAudioFileData(WebKit::WebAudioBus* destination_bus, const char* data,
12 size_t data_size, double sample_rate) { 93 size_t data_size, double sample_rate,
13 NOTIMPLEMENTED(); 94 const WebAudioMediaCodecRunner& runner) {
14 return false; 95 AudioDecoderIO audio_decoder(data, data_size);
96
97 if (!audio_decoder.is_valid()) {
bulach 2013/03/28 13:39:25 nit: remove {
98 return false;
99 }
100 base::SharedMemoryHandle encoded_data_handle;
101
102 audio_decoder.ShareEncodedToProcess(&encoded_data_handle);
103
104 DVLOG(0) << "DecodeAudioFileData: Starting MediaCodec";
105
106 base::FileDescriptor fd(audio_decoder.getWriteFd(), true);
bulach 2013/03/28 13:39:25 nit: remove extra space before "fd("
107
108 // Start MediaCodec which will read from encoded_data_handle for our
109 // shared memory and write the decoded pcm samples (16-bit integer)
110 // to our pipe.
111
112 runner.Run(encoded_data_handle, fd);
113
114 // First, read the number of channels, the sample rate, and the
115 // number of frames. This must be coordinated with
116 // WebAudioMediaCodecBridge!
117 //
118 // TODO(rtoy): If we know the number of samples, we can create the
119 // destination bus directly and do the conversion directly to the
120 // bus instead of buffering up everything before saving the data to
121 // the bus.
122
123 int input_fd = audio_decoder.getReadFd();
124 long info[4];
125
126 DVLOG(0) << "Reading audio file info from fd " << input_fd;
127 ssize_t nread = read(input_fd, info, sizeof(info));
128 DVLOG(0) << "read: " << nread << " bytes:\n"
129 << " 0: number of channels = " << info[0] << "\n"
130 << " 1: sample rate = " << info[1] << "\n"
131 << " 2: number of frames = " << info[2] << "\n"
132 << " 3: is vorbis = " << info[3];
133
134 if (nread != sizeof(info)) {
135 return false;
136 }
137
138 int number_of_channels = info[0];
139 unsigned long expected_number_of_samples = info[2] * number_of_channels;
140 double file_sample_rate = static_cast<double>(info[1]);
141 bool is_vorbis = static_cast<bool>(info[3]);
142
143 // Sanity checks
144 if (!number_of_channels ||
145 number_of_channels > media::limits::kMaxChannels ||
146 file_sample_rate < media::limits::kMinSampleRate ||
147 file_sample_rate > media::limits::kMaxSampleRate) {
148 return false;
149 }
150
151 short pipe_data[PIPE_BUF / sizeof(short)];
152 std::vector<short> decoded_samples;
153
154 // Keep reading from the pipe until it's closed.
155 while ((nread = read(input_fd, pipe_data, sizeof(pipe_data))) > 0) {
156 int nsamples = nread / sizeof(short);
157 decoded_samples.reserve(decoded_samples.size() + nsamples);
158 for (int k = 0; k < nsamples; ++k) {
159 decoded_samples.push_back(pipe_data[k]);
160 }
161 }
162
163 DVLOG(0) << "Total samples read = " << decoded_samples.size();
164
165 if (!is_vorbis &&
166 expected_number_of_samples &&
167 decoded_samples.size() != expected_number_of_samples) {
168 VLOG(0) << "Expected " << expected_number_of_samples
169 << " but received " << decoded_samples.size();
170 }
171
172 // Convert the samples and save them in the audio bus.
173 int number_of_samples = decoded_samples.size();
174 int number_of_frames = number_of_samples / number_of_channels;
175
176 destination_bus->initialize(number_of_channels,
177 number_of_frames,
178 file_sample_rate);
179
180 int decoded_frames = 0;
181 for (int m = 0; m < number_of_samples; m += number_of_channels) {
182 for (int k = 0; k < number_of_channels; ++k) {
183 destination_bus->channelData(k)[decoded_frames] =
184 decoded_samples[m + k] / 32768.0;
185 }
186 decoded_frames++;
bulach 2013/03/28 13:39:25 nit: ++decoded_frames;
187 }
188
189 return true;
15 } 190 }
16 191
17 } // namespace webkit_media 192 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698