OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/android/webaudio_media_codec_bridge.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <unistd.h> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_array.h" |
| 13 #include "base/android/jni_string.h" |
| 14 #include "base/basictypes.h" |
| 15 #include "base/logging.h" |
| 16 #include "jni/WebAudioMediaCodecBridge_jni.h" |
| 17 |
| 18 |
| 19 using base::android::AttachCurrentThread; |
| 20 |
| 21 namespace media { |
| 22 |
| 23 void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( |
| 24 base::SharedMemoryHandle encoded_audio_handle, |
| 25 base::FileDescriptor pcm_output) { |
| 26 DVLOG(0) << "RunWebAudioMediaCodec"; |
| 27 |
| 28 WebAudioMediaCodecBridge bridge(encoded_audio_handle, pcm_output); |
| 29 bool result = bridge.DecodeInMemoryAudioFile(); |
| 30 DVLOG(0) << "RunWebAudioMediaCodec returned " << result; |
| 31 } |
| 32 |
| 33 WebAudioMediaCodecBridge::WebAudioMediaCodecBridge( |
| 34 base::SharedMemoryHandle encoded_audio_handle, |
| 35 base::FileDescriptor pcm_output) |
| 36 : encoded_audio_handle_(encoded_audio_handle.fd), |
| 37 pcm_output_(pcm_output.fd) { |
| 38 DVLOG(0) << "WebAudioMediaCodecBridge start **********************" |
| 39 << "input fd = " << encoded_audio_handle_ |
| 40 << " output fd = " << pcm_output.fd; |
| 41 } |
| 42 |
| 43 WebAudioMediaCodecBridge::~WebAudioMediaCodecBridge() { |
| 44 int result = close(pcm_output_); |
| 45 if (result) { |
| 46 DVLOG(0) << "Couldn't close output fd " << pcm_output_; |
| 47 DVLOG(0) << strerror(errno); |
| 48 } |
| 49 |
| 50 result = close(encoded_audio_handle_); |
| 51 if (result) { |
| 52 DVLOG(0) << "Couldn't close shared mem fd " << encoded_audio_handle_; |
| 53 DVLOG(0) << strerror(errno); |
| 54 } |
| 55 } |
| 56 |
| 57 bool WebAudioMediaCodecBridge::DecodeInMemoryAudioFile() { |
| 58 // Process the encoded data from |encoded_data_handle_|. |
| 59 |
| 60 JNIEnv* env = AttachCurrentThread(); |
| 61 CHECK(env); |
| 62 jboolean decoded = Java_WebAudioMediaCodecBridge_decodeAudioFile( |
| 63 env, |
| 64 base::android::GetApplicationContext(), |
| 65 reinterpret_cast<intptr_t>(this), |
| 66 encoded_audio_handle_); |
| 67 |
| 68 DVLOG(0) << "decoded = " << static_cast<bool>(decoded); |
| 69 return decoded; |
| 70 } |
| 71 |
| 72 void WebAudioMediaCodecBridge::InitializeDestination( |
| 73 JNIEnv* env, |
| 74 jobject /*java object*/, |
| 75 jint channel_count, |
| 76 jint sample_rate, |
| 77 jlong duration_microsec, |
| 78 jboolean is_vorbis) { |
| 79 // Send information about this audio file: number of channels, |
| 80 // sample rate (Hz), number of frames, a flag indicating whether |
| 81 // this file is an audio/vorbis file. Information is sent as a set of |
| 82 // 4 longs. This must be coordinated with DecodeAudioFileData! |
| 83 |
| 84 unsigned long info[4] = {channel_count, |
| 85 sample_rate, |
| 86 // The number of frames is the duration of the file |
| 87 // (in microseconds) times the sample rate. |
| 88 0.5 + (duration_microsec * 0.000001 * sample_rate), |
| 89 is_vorbis ? 1 : 0}; |
| 90 |
| 91 DVLOG(0) << "InitializeDestination:" |
| 92 << " channel count = " << channel_count |
| 93 << " rate = " << sample_rate |
| 94 << " duration = " << duration_microsec << " microsec" |
| 95 << " vorbis = " << (is_vorbis ? "yes" : "no"); |
| 96 |
| 97 write(pcm_output_, info, sizeof(info)); |
| 98 } |
| 99 |
| 100 void WebAudioMediaCodecBridge::OnChunkDecoded( |
| 101 JNIEnv* env, |
| 102 jobject /*java object*/, |
| 103 jobject buf, |
| 104 jint buf_size) { |
| 105 int8_t* buffer = |
| 106 static_cast<int8_t*>(env->GetDirectBufferAddress(buf)); |
| 107 |
| 108 // Write out the data to the pipe in small chunks if necessary. |
| 109 while (buf_size > 0) { |
| 110 int bytes_to_write = (buf_size >= PIPE_BUF) ? PIPE_BUF : buf_size; |
| 111 ssize_t bytes_written = write(pcm_output_, buffer, bytes_to_write); |
| 112 if (bytes_written == -1) |
| 113 break; |
| 114 buf_size -= bytes_written; |
| 115 buffer += bytes_written; |
| 116 } |
| 117 } |
| 118 |
| 119 bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { |
| 120 return RegisterNativesImpl(env); |
| 121 } |
| 122 |
| 123 } // namespace |
OLD | NEW |