Index: media/base/android/webaudio_media_codec_bridge.cc |
diff --git a/media/base/android/webaudio_media_codec_bridge.cc b/media/base/android/webaudio_media_codec_bridge.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..640c1810879498876e2611b2b5f8f7fc7174fe0e |
--- /dev/null |
+++ b/media/base/android/webaudio_media_codec_bridge.cc |
@@ -0,0 +1,126 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/base/android/webaudio_media_codec_bridge.h" |
+ |
+#include <unistd.h> |
+#include <vector> |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_array.h" |
+#include "base/android/jni_string.h" |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "jni/WebAudioMediaCodecBridge_jni.h" |
+ |
+ |
+using base::android::AttachCurrentThread; |
+using base::android::DetachFromVM; |
felipeg
2013/03/29 17:18:29
this is not being used
|
+ |
+namespace media { |
+ |
+void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( |
+ base::SharedMemoryHandle input_fd, |
+ base::FileDescriptor output_fd) { |
+ DVLOG(0) << "RunWebAudioMediaCodec"; |
+ |
+ WebAudioMediaCodecBridge bridge(input_fd, output_fd); |
+ bool result = bridge.DecodeInMemoryAudioFile(); |
+ DVLOG(0) << "RunWebAudioMediaCodec returned " << result; |
+} |
+ |
+WebAudioMediaCodecBridge::WebAudioMediaCodecBridge( |
+ base::SharedMemoryHandle input_fd, |
+ base::FileDescriptor output_fd) |
+ : input_fd_(input_fd.fd), |
+ output_fd_(output_fd.fd) { |
+ |
felipeg
2013/03/29 17:18:29
drop blank
|
+ DVLOG(0) << "WebAudioMediaCodecBridge start **********************"; |
+ DVLOG(0) << "input fd = " << input_fd_ |
+ << " output fd = " << output_fd.fd; |
+} |
+ |
+WebAudioMediaCodecBridge::~WebAudioMediaCodecBridge() { |
+ int rc = close(output_fd_); |
felipeg
2013/03/29 17:18:29
s/rc/result or something meaningful
|
+ if (rc) |
+ VLOG(0) << "Couldn't close output fd " << output_fd_ |
+ << ": rc = " << rc; |
+ |
+ rc = close(input_fd_); |
+ if (rc) |
+ VLOG(0) << "Couldn't close shared mem fd " << input_fd_ |
+ << ": rc = " << rc; |
+} |
+ |
+bool WebAudioMediaCodecBridge::DecodeInMemoryAudioFile() { |
+ // Process the encoded data that is in shared memory given by the |
+ // file descriptor encodedDataFD_. |
+ |
+ JNIEnv* env = AttachCurrentThread(); |
+ CHECK(env); |
+ jboolean decoded = Java_WebAudioMediaCodecBridge_decodeAudioFile( |
+ env, |
+ base::android::GetApplicationContext(), |
+ reinterpret_cast<intptr_t>(this), |
+ input_fd_); |
+ |
+ DVLOG(0) << "decoded = " << static_cast<bool>(decoded); |
+ return decoded; |
+} |
+ |
+void WebAudioMediaCodecBridge::InitializeDestination( |
+ JNIEnv* env, |
+ jobject /*java object*/, |
+ jint number_of_channels, |
+ jint sample_rate, |
+ jlong duration_us, |
+ jboolean is_vorbis) { |
+ |
felipeg
2013/03/29 17:18:29
Drop blank line
|
+ long info[4]; |
+ info[0] = number_of_channels; |
+ info[1] = sample_rate; |
+ info[2] = 0.5 + (duration_us * 0.000001 * sample_rate); |
felipeg
2013/03/29 17:18:29
add a comment regarding the math here (I already f
|
+ info[3] = is_vorbis ? 1 : 0; |
+ |
+ DVLOG(0) << "InitializeDestination:"; |
+ DVLOG(0) << " number of channels = " << number_of_channels; |
+ DVLOG(0) << " rate = " << sample_rate; |
+ DVLOG(0) << " duration = " << duration_us << " us"; |
+ DVLOG(0) << " vorbis = " << (is_vorbis ? "yes" : "no"); |
+ |
+ write(output_fd_, info, sizeof(info)); |
+} |
+ |
+void WebAudioMediaCodecBridge::OnChunkDecoded( |
+ JNIEnv* env, |
+ jobject /*java object*/, |
+ jobject buf, |
+ jint buf_size) { |
+ |
felipeg
2013/03/29 17:18:29
drop blank
|
+ signed short* decoded_buffer = |
+ static_cast<signed short*>(env->GetDirectBufferAddress(buf)); |
+ DCHECK((buf_size % sizeof(decoded_buffer[0])) == 0); |
+ |
+ int bytes_left = buf_size; |
+ signed short* buffer = decoded_buffer; |
+ |
+ // Write out the data to the pipe atomically, in small chunks if |
felipeg
2013/03/29 17:18:29
what you mean by atomically ?
Raymond Toy (Google)
2013/03/29 19:47:24
Irrelevant here since we are the only writer of th
|
+ // necessary. |
+ while (bytes_left > 0) { |
+ int bytes_to_write = (bytes_left >= PIPE_BUF) ? PIPE_BUF : bytes_left; |
+ int bytes_written = write(output_fd_, buffer, bytes_to_write); |
+ if (bytes_written != bytes_to_write) |
+ VLOG(0) << "Only wrote " << bytes_written |
+ << " bytes but expected " << bytes_to_write; |
+ bytes_left -= bytes_written; |
+ buffer += bytes_written / sizeof(decoded_buffer[0]); |
+ } |
+} |
+ |
+bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { |
+ bool ret = RegisterNativesImpl(env); |
+ return ret; |
+} |
+ |
+} // namespace |