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

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, 8 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]; };
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 writes to
37 // pipefd_[0].
38 int pipefd_[2];
39
40 // True if everything was created correctly.
41 bool is_valid_;
42 };
43
44 AudioDecoderIO::AudioDecoderIO(const char* data, size_t data_size)
45 : is_valid_(true) {
46 pipefd_[0] = -1;
47 pipefd_[1] = -1;
48
49 // Create the shared memory and copy our data to it so that
50 // MediaCodec can access it.
51 encoded_shared_memory_.CreateAndMapAnonymous(data_size);
52
53 if (encoded_shared_memory_.memory()) {
54 memcpy(encoded_shared_memory_.memory(), data, data_size);
55
56 // Create a pipe for reading/writing the decoded pcm data
57 if (pipe(pipefd_)) {
58 // Pipe was not created
59 pipefd_[0] = -1;
60 pipefd_[1] = -1;
61 is_valid_ = false;
62 }
63 } else {
64 is_valid_ = false;
65 }
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())
98 return false;
99
felipeg 2013/03/29 17:18:29 you can also drop some blank lines here
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);
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);
felipeg 2013/03/29 17:18:29 I guess you cannot call directly GetWebAudioMediaC
Raymond Toy (Google) 2013/03/29 19:47:24 Yes, that creates an invalid dependency. This is
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 int number_of_channels = info[0];
138 unsigned long expected_number_of_samples = info[2] * number_of_channels;
139 double file_sample_rate = static_cast<double>(info[1]);
140 bool is_vorbis = static_cast<bool>(info[3]);
141
142 // Sanity checks
143 if (!number_of_channels ||
144 number_of_channels > media::limits::kMaxChannels ||
145 file_sample_rate < media::limits::kMinSampleRate ||
146 file_sample_rate > media::limits::kMaxSampleRate) {
147 return false;
148 }
149
150 short pipe_data[PIPE_BUF / sizeof(short)];
151 std::vector<short> decoded_samples;
152
153 // Keep reading from the pipe until it's closed.
154 while ((nread = read(input_fd, pipe_data, sizeof(pipe_data))) > 0) {
155 int nsamples = nread / sizeof(short);
156 decoded_samples.reserve(decoded_samples.size() + nsamples);
157 for (int k = 0; k < nsamples; ++k) {
158 decoded_samples.push_back(pipe_data[k]);
159 }
160 }
161
162 DVLOG(0) << "Total samples read = " << decoded_samples.size();
163
164 if (!is_vorbis &&
165 expected_number_of_samples &&
166 decoded_samples.size() != expected_number_of_samples) {
167 VLOG(0) << "Expected " << expected_number_of_samples
168 << " but received " << decoded_samples.size();
169 }
170
171 // Convert the samples and save them in the audio bus.
172 int number_of_samples = decoded_samples.size();
173 int number_of_frames = number_of_samples / number_of_channels;
174
175 destination_bus->initialize(number_of_channels,
176 number_of_frames,
177 file_sample_rate);
178
179 int decoded_frames = 0;
180 for (int m = 0; m < number_of_samples; m += number_of_channels) {
181 for (int k = 0; k < number_of_channels; ++k) {
182 destination_bus->channelData(k)[decoded_frames] =
183 decoded_samples[m + k] / 32768.0;
184 }
185 ++decoded_frames;
186 }
187
188 return true;
15 } 189 }
16 190
17 } // namespace webkit_media 191 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698