Chromium Code Reviews| 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..7c220f83ea1ed61b030015042e54d9f0d87838e0 |
| --- /dev/null |
| +++ b/media/base/android/webaudio_media_codec_bridge.cc |
| @@ -0,0 +1,127 @@ |
| +// 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; |
| + |
| +namespace media { |
| + |
| +void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( |
| + base::SharedMemoryHandle encoded_audio_handle, |
| + base::FileDescriptor pcm_output) { |
| + DVLOG(0) << "RunWebAudioMediaCodec"; |
| + |
| + WebAudioMediaCodecBridge bridge(encoded_audio_handle, pcm_output); |
| + bool result = bridge.DecodeInMemoryAudioFile(); |
|
acolwell GONE FROM CHROMIUM
2013/04/04 20:53:52
nit: Does DecodeInMemoryAudioFile() really need to
Raymond Toy (Google)
2013/04/04 22:38:05
It should probably be returned from RunWebAudioMed
|
| + DVLOG(0) << "RunWebAudioMediaCodec returned " << result; |
| +} |
| + |
| +WebAudioMediaCodecBridge::WebAudioMediaCodecBridge( |
| + base::SharedMemoryHandle encoded_audio_handle, |
| + base::FileDescriptor pcm_output) |
| + : encoded_audio_handle_(encoded_audio_handle.fd), |
| + pcm_output_(pcm_output.fd) { |
| + DVLOG(0) << "WebAudioMediaCodecBridge start **********************" |
| + << "input fd = " << encoded_audio_handle_ |
| + << " output fd = " << pcm_output.fd; |
| +} |
| + |
| +WebAudioMediaCodecBridge::~WebAudioMediaCodecBridge() { |
| + int result = close(pcm_output_); |
| + if (result) |
| + VLOG(0) << "Couldn't close output fd " << pcm_output_; |
|
acolwell GONE FROM CHROMIUM
2013/04/04 20:53:52
nit: DVLOG instead here and below?
|
| + |
| + result = close(encoded_audio_handle_); |
| + if (result) |
| + VLOG(0) << "Couldn't close shared mem fd " << encoded_audio_handle_; |
| +} |
| + |
| +bool WebAudioMediaCodecBridge::DecodeInMemoryAudioFile() { |
| + // Process the encoded data that is in shared memory given by the |
| + // file descriptor |encoded_data_handle_|. |
| + |
| + JNIEnv* env = AttachCurrentThread(); |
| + CHECK(env); |
| + jboolean decoded = Java_WebAudioMediaCodecBridge_decodeAudioFile( |
| + env, |
| + base::android::GetApplicationContext(), |
| + reinterpret_cast<intptr_t>(this), |
| + encoded_audio_handle_); |
| + |
| + DVLOG(0) << "decoded = " << static_cast<bool>(decoded); |
| + return decoded; |
| +} |
| + |
| +void WebAudioMediaCodecBridge::InitializeDestination( |
| + JNIEnv* env, |
| + jobject /*java object*/, |
| + jint channel_count, |
| + jint sample_rate, |
| + jlong duration_microsec, |
| + jboolean is_vorbis) { |
| + // Send information about this audio file: number of channels, |
| + // sample rate (Hz), number of frames, a flag indicating whether |
| + // this file is an audio/vorbis file. Information is sent as a set of |
| + // 4 longs. This must be coordinated with DecodeAudioFileData! |
| + |
| + long info[4] = {channel_count, |
| + sample_rate, |
| + // The number of frames is the duration of the file |
| + // (in microseconds) times the sample rate. |
| + 0.5 + (duration_microsec * 0.000001 * sample_rate), |
| + is_vorbis ? 1 : 0}; |
| + |
| + DVLOG(0) << "InitializeDestination:" |
| + << " channel count = " << channel_count |
| + << " rate = " << sample_rate |
| + << " duration = " << duration_microsec |
| + << " microsec vorbis= " |
| + << (is_vorbis ? "yes" : "no"); |
| + |
| + write(pcm_output_, info, sizeof(info)); |
| +} |
| + |
| +void WebAudioMediaCodecBridge::OnChunkDecoded( |
| + JNIEnv* env, |
| + jobject /*java object*/, |
| + jobject buf, |
| + jint buf_size) { |
| + int16_t* decoded_buffer = |
| + static_cast<int16_t*>(env->GetDirectBufferAddress(buf)); |
| + DCHECK((buf_size % sizeof(decoded_buffer[0])) == 0); |
| + |
| + int16_t* buffer = decoded_buffer; |
| + |
| + // Write out the data to the pipe in small chunks if necessary. |
| + while (buf_size > 0) { |
| + int bytes_to_write = (buf_size >= PIPE_BUF) ? PIPE_BUF : buf_size; |
| + ssize_t bytes_written = write(pcm_output_, buffer, bytes_to_write); |
| + if (bytes_written == -1) |
| + break; |
| + if (bytes_written != bytes_to_write) |
|
acolwell GONE FROM CHROMIUM
2013/04/04 20:53:52
nit: add { since this is a multi-line conditional
acolwell GONE FROM CHROMIUM
2013/04/04 20:53:52
nit: Add {} since this is a multi-line conditional
|
| + VLOG(0) << "Only wrote " << bytes_written |
| + << " bytes but expected " << bytes_to_write; |
| + buf_size -= bytes_written; |
| + buffer += bytes_written / sizeof(decoded_buffer[0]); |
| + } |
| +} |
| + |
| +bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { |
| + bool ret = RegisterNativesImpl(env); |
| + return ret; |
|
acolwell GONE FROM CHROMIUM
2013/04/04 20:53:52
nit: Change to "return RegisterNativesImpl(env);"
|
| +} |
| + |
| +} // namespace |