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

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 <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_;
DaleCurtis 2013/04/12 21:47:45 Needs DISALLOW_COPY_ASSIGN, etc.
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) {
DaleCurtis 2013/04/12 21:47:45 && close(read_fd_) ?
77 if (close(read_fd_)) {
78 DVLOG(0) << "Cannot close read fd " << read_fd_
79 << ": " << strerror(errno);
80 }
81 }
82 }
83
84 bool AudioDecoderIO::IsValid() const {
85 return read_fd_ >= 0 && write_fd_ >= 0 &&
86 encoded_shared_memory_.memory() ;
DaleCurtis 2013/04/12 21:47:45 spaces at end?
87 }
88
89 bool AudioDecoderIO::ShareEncodedToProcess(base::SharedMemoryHandle* handle) {
90 return encoded_shared_memory_.ShareToProcess(
91 base::Process::Current().handle(),
92 handle);
93 }
94
95 // To decode audio data, we want to use the Android MediaCodec class.
96 // But this can't run in a sandboxed process so we need initiate the
97 // request to MediaCodec in the browser. To do this, we create a
98 // shared memory buffer that holds the audio data. We send a message
99 // to the browser to start the decoder using this buffer and one end
100 // of a pipe. The MediaCodec class will decode the data from the
101 // shared memory and write the PCM samples back to us over a pipe.
11 bool DecodeAudioFileData(WebKit::WebAudioBus* destination_bus, const char* data, 102 bool DecodeAudioFileData(WebKit::WebAudioBus* destination_bus, const char* data,
12 size_t data_size, double sample_rate) { 103 size_t data_size, double sample_rate,
13 NOTIMPLEMENTED(); 104 const WebAudioMediaCodecRunner& runner) {
14 return false; 105 AudioDecoderIO audio_decoder(data, data_size);
106
107 if (!audio_decoder.IsValid())
108 return false;
109
110 base::SharedMemoryHandle encoded_data_handle;
111 audio_decoder.ShareEncodedToProcess(&encoded_data_handle);
112 base::FileDescriptor fd(audio_decoder.write_fd(), true);
113
114 DVLOG(0) << "DecodeAudioFileData: Starting MediaCodec";
115
116 // Start MediaCodec processing in the browser which will read from
117 // encoded_data_handle for our shared memory and write the decoded
118 // PCM samples (16-bit integer) to our pipe.
119
120 runner.Run(encoded_data_handle, fd);
121
122 // First, read the number of channels, the sample rate, and the
123 // number of frames and a flag indicating if the file is an
124 // ogg/vorbis file. This must be coordinated with
125 // WebAudioMediaCodecBridge!
126 //
127 // TODO(rtoy): If we know the number of samples, we can create the
128 // destination bus directly and do the conversion directly to the
129 // bus instead of buffering up everything before saving the data to
130 // the bus.
131
132 int input_fd = audio_decoder.read_fd();
133 unsigned long info[4];
134
135 DVLOG(0) << "Reading audio file info from fd " << input_fd;
136 ssize_t nread = HANDLE_EINTR(read(input_fd, info, sizeof(info)));
137 DVLOG(0) << "read: " << nread << " bytes:\n"
138 << " 0: number of channels = " << info[0] << "\n"
139 << " 1: sample rate = " << info[1] << "\n"
140 << " 2: number of frames = " << info[2] << "\n"
141 << " 3: is vorbis = " << info[3];
142
143 if (nread != sizeof(info))
144 return false;
145
146 unsigned number_of_channels = info[0];
147 double file_sample_rate = static_cast<double>(info[1]);
148
149 // Sanity checks
150 if (!number_of_channels ||
151 number_of_channels > media::limits::kMaxChannels ||
152 file_sample_rate < media::limits::kMinSampleRate ||
153 file_sample_rate > media::limits::kMaxSampleRate) {
154 return false;
155 }
156
157 int16_t pipe_data[PIPE_BUF / sizeof(int16_t)];
158 std::vector<int16_t> decoded_samples;
DaleCurtis 2013/04/12 21:47:45 should probably pre allocate this to the expected
Raymond Toy (Google) 2013/04/12 22:12:47 Can't do this right now because some audio files r
159
160 // Keep reading from the pipe until it's closed.
161 while ((nread =
162 HANDLE_EINTR(read(input_fd, pipe_data, sizeof(pipe_data)))) > 0) {
163 size_t nsamples = nread / sizeof(int16_t);
164 decoded_samples.reserve(decoded_samples.size() + nsamples);
165 for (size_t k = 0; k < nsamples; ++k) {
166 decoded_samples.push_back(pipe_data[k]);
167 }
168 }
169
170 DVLOG(0) << "Total samples read = " << decoded_samples.size();
171
172 // Convert the samples and save them in the audio bus.
173 size_t number_of_samples = decoded_samples.size();
174 size_t 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 size_t decoded_frames = 0;
181 for (size_t m = 0; m < number_of_samples; m += number_of_channels) {
182 for (size_t 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;
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