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 | |
20 namespace media { | |
21 | |
22 void WebAudioMediaCodecBridge::RunWebAudioMediaCodec( | |
23 base::SharedMemoryHandle encoded_audio_handle, | |
24 base::FileDescriptor pcm_output) { | |
25 DVLOG(0) << "RunWebAudioMediaCodec"; | |
26 | |
27 WebAudioMediaCodecBridge bridge(encoded_audio_handle, pcm_output); | |
28 bool result = bridge.DecodeInMemoryAudioFile(); | |
29 DVLOG(0) << "RunWebAudioMediaCodec returned " << result; | |
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(0) << "WebAudioMediaCodecBridge start **********************" | |
38 << "input fd = " << encoded_audio_handle_ | |
39 << " output fd = " << pcm_output.fd; | |
40 } | |
41 | |
42 WebAudioMediaCodecBridge::~WebAudioMediaCodecBridge() { | |
43 int result = close(pcm_output_); | |
44 if (result) | |
45 DVLOG(0) << "Couldn't close output fd " << pcm_output_; | |
palmer
2013/04/10 00:57:53
In both of these DVLOG messages, maybe it makes se
| |
46 | |
47 result = close(encoded_audio_handle_); | |
48 if (result) | |
49 DVLOG(0) << "Couldn't close shared mem fd " << encoded_audio_handle_; | |
50 } | |
51 | |
52 bool WebAudioMediaCodecBridge::DecodeInMemoryAudioFile() { | |
53 // Process the encoded data that is in shared memory given by the | |
palmer
2013/04/10 00:57:53
NIT: You could be more concise here. "Process the
| |
54 // file descriptor |encoded_data_handle_|. | |
55 | |
56 JNIEnv* env = AttachCurrentThread(); | |
57 CHECK(env); | |
58 jboolean decoded = Java_WebAudioMediaCodecBridge_decodeAudioFile( | |
59 env, | |
60 base::android::GetApplicationContext(), | |
61 reinterpret_cast<intptr_t>(this), | |
62 encoded_audio_handle_); | |
63 | |
64 DVLOG(0) << "decoded = " << static_cast<bool>(decoded); | |
65 return decoded; | |
66 } | |
67 | |
68 void WebAudioMediaCodecBridge::InitializeDestination( | |
69 JNIEnv* env, | |
70 jobject /*java object*/, | |
71 jint channel_count, | |
72 jint sample_rate, | |
73 jlong duration_microsec, | |
74 jboolean is_vorbis) { | |
75 // Send information about this audio file: number of channels, | |
76 // sample rate (Hz), number of frames, a flag indicating whether | |
77 // this file is an audio/vorbis file. Information is sent as a set of | |
78 // 4 longs. This must be coordinated with DecodeAudioFileData! | |
79 | |
80 long info[4] = {channel_count, | |
81 sample_rate, | |
82 // The number of frames is the duration of the file | |
83 // (in microseconds) times the sample rate. | |
84 0.5 + (duration_microsec * 0.000001 * sample_rate), | |
85 is_vorbis ? 1 : 0}; | |
86 | |
87 DVLOG(0) << "InitializeDestination:" | |
88 << " channel count = " << channel_count | |
89 << " rate = " << sample_rate | |
90 << " duration = " << duration_microsec | |
91 << " microsec vorbis= " | |
palmer
2013/04/10 00:57:53
Did you mean just "vorbis" here?
Raymond Toy (Google)
2013/04/10 19:23:36
I wanted the duration to include the units. I'll
| |
92 << (is_vorbis ? "yes" : "no"); | |
93 | |
94 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 int16_t* decoded_buffer = | |
103 static_cast<int16_t*>(env->GetDirectBufferAddress(buf)); | |
104 DCHECK((buf_size % sizeof(decoded_buffer[0])) == 0); | |
105 | |
106 int16_t* buffer = decoded_buffer; | |
palmer
2013/04/10 00:57:53
I still don't think you need both |buffer| and |de
| |
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 if (bytes_written != bytes_to_write) { | |
palmer
2013/04/10 00:57:53
This isn't really an exceptional condition worthy
| |
115 VLOG(0) << "Only wrote " << bytes_written | |
116 << " bytes but expected " << bytes_to_write; | |
117 } | |
118 buf_size -= bytes_written; | |
119 buffer += bytes_written / sizeof(decoded_buffer[0]); | |
palmer
2013/04/10 00:57:53
It's a bit confusing that we write in terms of byt
| |
120 } | |
121 } | |
122 | |
123 bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { | |
124 return RegisterNativesImpl(env); | |
125 } | |
126 | |
127 } // namespace | |
OLD | NEW |