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 "media/base/android/webaudio_media_codec_bridge.h" | 5 #include "media/base/android/webaudio_media_codec_bridge.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/android/jni_android.h" | 14 #include "base/android/jni_android.h" |
15 #include "base/android/jni_array.h" | 15 #include "base/android/jni_array.h" |
16 #include "base/android/jni_string.h" | 16 #include "base/android/jni_string.h" |
17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
| 20 #include "base/stl_util.h" |
20 #include "jni/WebAudioMediaCodecBridge_jni.h" | 21 #include "jni/WebAudioMediaCodecBridge_jni.h" |
21 #include "media/base/android/webaudio_media_codec_info.h" | 22 #include "media/base/android/webaudio_media_codec_info.h" |
22 | 23 |
23 | 24 |
24 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
25 | 26 |
26 namespace media { | 27 namespace media { |
27 | 28 |
28 void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( | 29 void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( |
29 base::SharedMemoryHandle encoded_audio_handle, | 30 base::SharedMemoryHandle encoded_audio_handle, |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 << " rate = " << sample_rate | 142 << " rate = " << sample_rate |
142 << " duration = " << duration_microsec << " microsec"; | 143 << " duration = " << duration_microsec << " microsec"; |
143 | 144 |
144 HANDLE_EINTR(write(pcm_output_, &info, sizeof(info))); | 145 HANDLE_EINTR(write(pcm_output_, &info, sizeof(info))); |
145 } | 146 } |
146 | 147 |
147 void WebAudioMediaCodecBridge::OnChunkDecoded( | 148 void WebAudioMediaCodecBridge::OnChunkDecoded( |
148 JNIEnv* env, | 149 JNIEnv* env, |
149 jobject /*java object*/, | 150 jobject /*java object*/, |
150 jobject buf, | 151 jobject buf, |
151 jint buf_size) { | 152 jint buf_size, |
| 153 jint input_channel_count, |
| 154 jint output_channel_count) { |
152 | 155 |
153 if (buf_size <= 0 || !buf) | 156 if (buf_size <= 0 || !buf) |
154 return; | 157 return; |
155 | 158 |
156 int8_t* buffer = | 159 int8_t* buffer = |
157 static_cast<int8_t*>(env->GetDirectBufferAddress(buf)); | 160 static_cast<int8_t*>(env->GetDirectBufferAddress(buf)); |
| 161 size_t count = static_cast<size_t>(buf_size); |
| 162 std::vector<int16_t> decoded_data; |
158 | 163 |
159 size_t count = static_cast<size_t>(buf_size); | 164 if (input_channel_count == 1 && output_channel_count == 2) { |
| 165 // See crbug.com/266006. The file has one channel, but the |
| 166 // decoder decided to return two channels. To be consistent with |
| 167 // the number of channels in the file, only send one channel (the |
| 168 // first). |
| 169 int16_t* data = static_cast<int16_t*>(env->GetDirectBufferAddress(buf)); |
| 170 int frame_count = buf_size / sizeof(*data) / 2; |
| 171 |
| 172 decoded_data.resize(frame_count); |
| 173 for (int k = 0; k < frame_count; ++k) { |
| 174 decoded_data[k] = *data; |
| 175 data += 2; |
| 176 } |
| 177 buffer = reinterpret_cast<int8_t*>(vector_as_array(&decoded_data)); |
| 178 DCHECK(buffer); |
| 179 count = frame_count * sizeof(*data); |
| 180 } |
160 | 181 |
161 // Write out the data to the pipe in small chunks if necessary. | 182 // Write out the data to the pipe in small chunks if necessary. |
162 while (count > 0) { | 183 while (count > 0) { |
163 int bytes_to_write = (count >= PIPE_BUF) ? PIPE_BUF : count; | 184 int bytes_to_write = (count >= PIPE_BUF) ? PIPE_BUF : count; |
164 ssize_t bytes_written = HANDLE_EINTR(write(pcm_output_, | 185 ssize_t bytes_written = HANDLE_EINTR(write(pcm_output_, |
165 buffer, | 186 buffer, |
166 bytes_to_write)); | 187 bytes_to_write)); |
167 if (bytes_written == -1) | 188 if (bytes_written == -1) |
168 break; | 189 break; |
169 count -= bytes_written; | 190 count -= bytes_written; |
170 buffer += bytes_written; | 191 buffer += bytes_written; |
171 } | 192 } |
172 } | 193 } |
173 | 194 |
174 bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { | 195 bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { |
175 return RegisterNativesImpl(env); | 196 return RegisterNativesImpl(env); |
176 } | 197 } |
177 | 198 |
178 } // namespace | 199 } // namespace |
OLD | NEW |