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

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

Powered by Google App Engine
This is Rietveld 408576698