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 is_valid_; } | |
28 int GetReadFd() const { return pipefd_[0]; } | |
acolwell GONE FROM CHROMIUM
2013/04/04 20:53:52
nit: How about just replacing pipefd_ with read_fd
| |
29 int GetWriteFd() const { 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.IsValid()) | |
98 return false; | |
99 | |
100 base::SharedMemoryHandle encoded_data_handle; | |
101 audio_decoder.ShareEncodedToProcess(&encoded_data_handle); | |
102 base::FileDescriptor fd(audio_decoder.GetWriteFd(), true); | |
103 | |
104 DVLOG(0) << "DecodeAudioFileData: Starting MediaCodec"; | |
105 | |
106 // Start MediaCodec processing in the browser which will read from | |
107 // encoded_data_handle for our shared memory and write the decoded | |
108 // pcm samples (16-bit integer) to our pipe. | |
109 | |
110 runner.Run(encoded_data_handle, fd); | |
111 | |
112 // First, read the number of channels, the sample rate, and the | |
113 // number of frames and a flag indicating if the file is an | |
114 // ogg/vorbis file. This must be coordinated with | |
115 // WebAudioMediaCodecBridge! | |
116 // | |
117 // TODO(rtoy): If we know the number of samples, we can create the | |
118 // destination bus directly and do the conversion directly to the | |
119 // bus instead of buffering up everything before saving the data to | |
120 // the bus. | |
121 | |
122 int input_fd = audio_decoder.GetReadFd(); | |
123 long info[4]; | |
124 | |
125 DVLOG(0) << "Reading audio file info from fd " << input_fd; | |
126 ssize_t nread = read(input_fd, info, sizeof(info)); | |
127 DVLOG(0) << "read: " << nread << " bytes:\n" | |
128 << " 0: number of channels = " << info[0] << "\n" | |
129 << " 1: sample rate = " << info[1] << "\n" | |
130 << " 2: number of frames = " << info[2] << "\n" | |
131 << " 3: is vorbis = " << info[3]; | |
132 | |
133 if (nread != sizeof(info)) | |
134 return false; | |
135 | |
136 int number_of_channels = info[0]; | |
137 unsigned long expected_number_of_samples = info[2] * number_of_channels; | |
138 double file_sample_rate = static_cast<double>(info[1]); | |
139 bool is_vorbis = static_cast<bool>(info[3]); | |
140 | |
141 // Sanity checks | |
142 if (!number_of_channels || | |
143 number_of_channels > media::limits::kMaxChannels || | |
144 file_sample_rate < media::limits::kMinSampleRate || | |
145 file_sample_rate > media::limits::kMaxSampleRate) { | |
146 return false; | |
147 } | |
148 | |
149 short pipe_data[PIPE_BUF / sizeof(short)]; | |
150 std::vector<short> decoded_samples; | |
151 | |
152 // Keep reading from the pipe until it's closed. | |
153 while ((nread = read(input_fd, pipe_data, sizeof(pipe_data))) > 0) { | |
154 int nsamples = nread / sizeof(short); | |
155 decoded_samples.reserve(decoded_samples.size() + nsamples); | |
156 for (int k = 0; k < nsamples; ++k) { | |
157 decoded_samples.push_back(pipe_data[k]); | |
158 } | |
159 } | |
160 | |
161 DVLOG(0) << "Total samples read = " << decoded_samples.size(); | |
162 | |
163 if (!is_vorbis && | |
164 expected_number_of_samples && | |
165 decoded_samples.size() != expected_number_of_samples) { | |
166 VLOG(0) << "Expected " << expected_number_of_samples | |
167 << " but received " << decoded_samples.size(); | |
168 } | |
169 | |
170 // Convert the samples and save them in the audio bus. | |
171 int number_of_samples = decoded_samples.size(); | |
172 int number_of_frames = number_of_samples / number_of_channels; | |
173 | |
174 destination_bus->initialize(number_of_channels, | |
175 number_of_frames, | |
176 file_sample_rate); | |
177 | |
178 int decoded_frames = 0; | |
179 for (int m = 0; m < number_of_samples; m += number_of_channels) { | |
180 for (int k = 0; k < number_of_channels; ++k) { | |
181 destination_bus->channelData(k)[decoded_frames] = | |
182 decoded_samples[m + k] / 32768.0; | |
183 } | |
184 ++decoded_frames; | |
185 } | |
186 | |
187 return true; | |
15 } | 188 } |
16 | 189 |
17 } // namespace webkit_media | 190 } // namespace webkit_media |
OLD | NEW |