Chromium Code Reviews| 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 <unistd.h> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/android/jni_android.h" | |
| 11 #include "base/android/jni_array.h" | |
| 12 #include "base/android/jni_string.h" | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "jni/WebAudioMediaCodecBridge_jni.h" | |
| 16 | |
| 17 | |
| 18 using base::android::AttachCurrentThread; | |
| 19 using base::android::DetachFromVM; | |
|
felipeg
2013/03/29 17:18:29
this is not being used
| |
| 20 | |
| 21 namespace media { | |
| 22 | |
| 23 void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( | |
| 24 base::SharedMemoryHandle input_fd, | |
| 25 base::FileDescriptor output_fd) { | |
| 26 DVLOG(0) << "RunWebAudioMediaCodec"; | |
| 27 | |
| 28 WebAudioMediaCodecBridge bridge(input_fd, output_fd); | |
| 29 bool result = bridge.DecodeInMemoryAudioFile(); | |
| 30 DVLOG(0) << "RunWebAudioMediaCodec returned " << result; | |
| 31 } | |
| 32 | |
| 33 WebAudioMediaCodecBridge::WebAudioMediaCodecBridge( | |
| 34 base::SharedMemoryHandle input_fd, | |
| 35 base::FileDescriptor output_fd) | |
| 36 : input_fd_(input_fd.fd), | |
| 37 output_fd_(output_fd.fd) { | |
| 38 | |
|
felipeg
2013/03/29 17:18:29
drop blank
| |
| 39 DVLOG(0) << "WebAudioMediaCodecBridge start **********************"; | |
| 40 DVLOG(0) << "input fd = " << input_fd_ | |
| 41 << " output fd = " << output_fd.fd; | |
| 42 } | |
| 43 | |
| 44 WebAudioMediaCodecBridge::~WebAudioMediaCodecBridge() { | |
| 45 int rc = close(output_fd_); | |
|
felipeg
2013/03/29 17:18:29
s/rc/result or something meaningful
| |
| 46 if (rc) | |
| 47 VLOG(0) << "Couldn't close output fd " << output_fd_ | |
| 48 << ": rc = " << rc; | |
| 49 | |
| 50 rc = close(input_fd_); | |
| 51 if (rc) | |
| 52 VLOG(0) << "Couldn't close shared mem fd " << input_fd_ | |
| 53 << ": rc = " << rc; | |
| 54 } | |
| 55 | |
| 56 bool WebAudioMediaCodecBridge::DecodeInMemoryAudioFile() { | |
| 57 // Process the encoded data that is in shared memory given by the | |
| 58 // file descriptor encodedDataFD_. | |
| 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 input_fd_); | |
| 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 number_of_channels, | |
| 76 jint sample_rate, | |
| 77 jlong duration_us, | |
| 78 jboolean is_vorbis) { | |
| 79 | |
|
felipeg
2013/03/29 17:18:29
Drop blank line
| |
| 80 long info[4]; | |
| 81 info[0] = number_of_channels; | |
| 82 info[1] = sample_rate; | |
| 83 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
| |
| 84 info[3] = is_vorbis ? 1 : 0; | |
| 85 | |
| 86 DVLOG(0) << "InitializeDestination:"; | |
| 87 DVLOG(0) << " number of channels = " << number_of_channels; | |
| 88 DVLOG(0) << " rate = " << sample_rate; | |
| 89 DVLOG(0) << " duration = " << duration_us << " us"; | |
| 90 DVLOG(0) << " vorbis = " << (is_vorbis ? "yes" : "no"); | |
| 91 | |
| 92 write(output_fd_, info, sizeof(info)); | |
| 93 } | |
| 94 | |
| 95 void WebAudioMediaCodecBridge::OnChunkDecoded( | |
| 96 JNIEnv* env, | |
| 97 jobject /*java object*/, | |
| 98 jobject buf, | |
| 99 jint buf_size) { | |
| 100 | |
|
felipeg
2013/03/29 17:18:29
drop blank
| |
| 101 signed short* decoded_buffer = | |
| 102 static_cast<signed short*>(env->GetDirectBufferAddress(buf)); | |
| 103 DCHECK((buf_size % sizeof(decoded_buffer[0])) == 0); | |
| 104 | |
| 105 int bytes_left = buf_size; | |
| 106 signed short* buffer = decoded_buffer; | |
| 107 | |
| 108 // 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
| |
| 109 // necessary. | |
| 110 while (bytes_left > 0) { | |
| 111 int bytes_to_write = (bytes_left >= PIPE_BUF) ? PIPE_BUF : bytes_left; | |
| 112 int bytes_written = write(output_fd_, buffer, bytes_to_write); | |
| 113 if (bytes_written != bytes_to_write) | |
| 114 VLOG(0) << "Only wrote " << bytes_written | |
| 115 << " bytes but expected " << bytes_to_write; | |
| 116 bytes_left -= bytes_written; | |
| 117 buffer += bytes_written / sizeof(decoded_buffer[0]); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { | |
| 122 bool ret = RegisterNativesImpl(env); | |
| 123 return ret; | |
| 124 } | |
| 125 | |
| 126 } // namespace | |
| OLD | NEW |