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