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/MediaCodec_jni.h" | |
bulach
2013/03/28 13:39:25
it seems this is unused..
| |
16 #include "jni/WebAudioMediaCodecBridge_jni.h" | |
17 | |
18 | |
19 using base::android::AttachCurrentThread; | |
20 using base::android::DetachFromVM; | |
21 | |
22 namespace media { | |
23 | |
24 void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( | |
25 base::SharedMemoryHandle input_fd, | |
26 base::FileDescriptor output_fd) { | |
27 DVLOG(0) << "RunWebAudioMediaCodec"; | |
28 | |
29 WebAudioMediaCodecBridge bridge(input_fd, output_fd); | |
30 bool result = bridge.DecodeInMemoryAudioFile(); | |
31 DVLOG(0) << "RunWebAudioMediaCodec returned " << result; | |
32 } | |
33 | |
34 WebAudioMediaCodecBridge::WebAudioMediaCodecBridge( | |
35 base::SharedMemoryHandle input_fd, | |
36 base::FileDescriptor output_fd) | |
37 : input_fd_(input_fd.fd), | |
38 output_fd_(output_fd.fd) { | |
39 | |
40 DVLOG(0) << "WebAudioMediaCodecBridge start **********************"; | |
41 DVLOG(0) << "input fd = " << input_fd_ | |
42 << " output fd = " << output_fd.fd; | |
43 env_ = AttachCurrentThread(); | |
bulach
2013/03/28 13:39:25
nit: as above, avoid caching env and context.
| |
44 CHECK(env_); | |
45 | |
46 j_context_ = base::android::GetApplicationContext(); | |
47 | |
48 } | |
49 | |
50 WebAudioMediaCodecBridge::~WebAudioMediaCodecBridge() { | |
51 DVLOG(0) << "closing output fd " << output_fd_; | |
52 int rc = close(output_fd_); | |
53 if (rc != 0) { | |
bulach
2013/03/28 13:39:25
nit:
if (rc)
VLOG(0) << "Couldn't close output f
| |
54 VLOG(0) << "Couldn't close output fd: rc = " << rc; | |
55 } | |
56 DVLOG(0) << "Closing shared memory fd " << input_fd_; | |
57 rc = close(input_fd_); | |
58 if (rc != 0) { | |
bulach
2013/03/28 13:39:25
as above..
| |
59 VLOG(0) << "Couldn't close shared mem fd: rc = " << rc; | |
60 } | |
61 } | |
62 | |
63 bool WebAudioMediaCodecBridge::DecodeInMemoryAudioFile() { | |
64 // Process the encoded data that is in shared memory given by the | |
65 // file descriptor encodedDataFD_. | |
66 | |
67 jboolean decoded = Java_WebAudioMediaCodecBridge_decodeAudioFile( | |
68 env_, | |
69 j_context_, | |
70 reinterpret_cast<intptr_t>(this), | |
71 input_fd_); | |
72 | |
73 DVLOG(0) << "decoded = " << static_cast<bool>(decoded); | |
74 return decoded; | |
75 } | |
76 | |
77 void WebAudioMediaCodecBridge::InitializeDestination( | |
78 JNIEnv* env, | |
79 jobject /*java object*/, | |
80 jint number_of_channels, | |
81 jint sample_rate, | |
82 jlong duration_us, | |
83 jboolean is_vorbis) { | |
84 | |
85 long info[4]; | |
86 info[0] = number_of_channels; | |
87 info[1] = sample_rate; | |
88 info[2] = 0.5 + (duration_us * 0.000001 * sample_rate); | |
bulach
2013/03/28 13:39:25
since this is in integer space, how about:
const l
Raymond Toy (Google)
2013/03/28 17:38:31
I think it's clearer to do a floating-point conver
| |
89 info[3] = is_vorbis ? 1 : 0; | |
90 | |
91 DVLOG(0) << "InitializeDestination:"; | |
92 DVLOG(0) << " number of channels = " << number_of_channels; | |
93 DVLOG(0) << " rate = " << sample_rate; | |
94 DVLOG(0) << " duration = " << duration_us << " us"; | |
95 DVLOG(0) << " vorbis = " << (is_vorbis ? "yes" : "no"); | |
96 | |
97 write(output_fd_, info, sizeof(info)); | |
98 } | |
99 | |
100 void WebAudioMediaCodecBridge::OnChunkDecoded( | |
101 JNIEnv* env, | |
102 jobject /*java object*/, | |
103 jobject buf, | |
104 jint buf_size) { | |
105 | |
106 signed short* decoded_buffer = | |
107 static_cast<signed short*>(env->GetDirectBufferAddress(buf)); | |
108 DCHECK((buf_size % sizeof(decoded_buffer[0])) == 0); | |
109 | |
110 int bytes_left = buf_size; | |
111 signed short* buffer = decoded_buffer; | |
112 | |
113 // Write out the data to the pipe atomically, in small chunks if | |
114 // necessary. | |
115 while (bytes_left > 0) { | |
116 int nwrite = (bytes_left >= PIPE_BUF) ? PIPE_BUF : bytes_left; | |
117 write(output_fd_, buffer, nwrite); | |
bulach
2013/03/28 13:39:25
question: perhaps check the return value was actua
| |
118 bytes_left -= nwrite; | |
119 buffer += nwrite / sizeof(decoded_buffer[0]); | |
120 } | |
121 } | |
122 | |
123 bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { | |
124 bool ret = RegisterNativesImpl(env); | |
125 DCHECK(g_WebAudioMediaCodecBridge_clazz); | |
126 if (ret) | |
127 ret = JNI_MediaCodec::RegisterNativesImpl(env); | |
bulach
2013/03/28 13:39:25
as above, this seems unused, also there's no need
| |
128 return ret; | |
129 } | |
130 | |
131 } // namespace | |
OLD | NEW |