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 input_fd, | |
24 base::FileDescriptor output_fd) { | |
25 DVLOG(0) << "RunWebAudioMediaCodec"; | |
26 | |
27 WebAudioMediaCodecBridge bridge(input_fd, output_fd); | |
28 bool result = bridge.DecodeInMemoryAudioFile(); | |
29 DVLOG(0) << "RunWebAudioMediaCodec returned " << result; | |
30 } | |
31 | |
32 WebAudioMediaCodecBridge::WebAudioMediaCodecBridge( | |
33 base::SharedMemoryHandle input_fd, | |
34 base::FileDescriptor output_fd) | |
35 : input_fd_(input_fd.fd), | |
36 output_fd_(output_fd.fd) { | |
37 DVLOG(0) << "WebAudioMediaCodecBridge start **********************"; | |
palmer
2013/04/02 18:01:08
Combine these two DVLOG statements into one.
| |
38 DVLOG(0) << "input fd = " << input_fd_ | |
39 << " output fd = " << output_fd.fd; | |
40 } | |
41 | |
42 WebAudioMediaCodecBridge::~WebAudioMediaCodecBridge() { | |
43 int result = close(output_fd_); | |
44 if (result) | |
45 VLOG(0) << "Couldn't close output fd " << output_fd_ | |
palmer
2013/04/02 18:01:08
In both of these VLOGs, would it help to use strer
| |
46 << ": result = " << result; | |
47 | |
48 result = close(input_fd_); | |
49 if (result) | |
50 VLOG(0) << "Couldn't close shared mem fd " << input_fd_ | |
51 << ": result = " << result; | |
52 } | |
53 | |
54 bool WebAudioMediaCodecBridge::DecodeInMemoryAudioFile() { | |
55 // Process the encoded data that is in shared memory given by the | |
56 // file descriptor encodedDataFD_. | |
57 | |
58 JNIEnv* env = AttachCurrentThread(); | |
59 CHECK(env); | |
60 jboolean decoded = Java_WebAudioMediaCodecBridge_decodeAudioFile( | |
61 env, | |
62 base::android::GetApplicationContext(), | |
63 reinterpret_cast<intptr_t>(this), | |
64 input_fd_); | |
65 | |
66 DVLOG(0) << "decoded = " << static_cast<bool>(decoded); | |
67 return decoded; | |
68 } | |
69 | |
70 void WebAudioMediaCodecBridge::InitializeDestination( | |
71 JNIEnv* env, | |
72 jobject /*java object*/, | |
73 jint number_of_channels, | |
74 jint sample_rate, | |
75 jlong duration_us, | |
76 jboolean is_vorbis) { | |
77 long info[4]; | |
palmer
2013/04/02 18:01:08
NIT: I think you can combine the declaration with
Raymond Toy (Google)
2013/04/02 21:52:21
It's long so we can handle very long audio files,
| |
78 | |
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 ogg/vorbis file. Information is sent as a set of | |
palmer
2013/04/02 18:01:08
Use the same MIME type string as elsewhere; I thin
| |
82 // 4 longs. This must be coordinated with DecodeAudioFileData! | |
83 info[0] = number_of_channels; | |
84 info[1] = sample_rate; | |
85 // The number of frames is the length of the file (in us) times the | |
palmer
2013/04/02 18:01:08
NIT: "in microseconds"
| |
86 // sample rate. | |
87 info[2] = 0.5 + (duration_us * 0.000001 * sample_rate); | |
palmer
2013/04/02 18:01:08
NIT: Only one space after *.
| |
88 info[3] = is_vorbis ? 1 : 0; | |
89 | |
90 DVLOG(0) << "InitializeDestination:"; | |
palmer
2013/04/02 18:01:08
Combine these into one statement.
| |
91 DVLOG(0) << " number of channels = " << number_of_channels; | |
92 DVLOG(0) << " rate = " << sample_rate; | |
93 DVLOG(0) << " duration = " << duration_us << " us"; | |
94 DVLOG(0) << " vorbis = " << (is_vorbis ? "yes" : "no"); | |
95 | |
96 write(output_fd_, info, sizeof(info)); | |
97 } | |
98 | |
99 void WebAudioMediaCodecBridge::OnChunkDecoded( | |
100 JNIEnv* env, | |
101 jobject /*java object*/, | |
102 jobject buf, | |
103 jint buf_size) { | |
104 signed short* decoded_buffer = | |
palmer
2013/04/02 18:01:08
Do you need the signed keyword?
Also, might it be
| |
105 static_cast<signed short*>(env->GetDirectBufferAddress(buf)); | |
106 DCHECK((buf_size % sizeof(decoded_buffer[0])) == 0); | |
107 | |
108 int bytes_left = buf_size; | |
palmer
2013/04/02 18:01:08
Not sure you need this; just use buf_size.
| |
109 signed short* buffer = decoded_buffer; | |
palmer
2013/04/02 18:01:08
You don't really need to have both |buffer| and |d
| |
110 | |
111 // Write out the data to the pipe in small chunks if necessary. | |
112 while (bytes_left > 0) { | |
113 int bytes_to_write = (bytes_left >= PIPE_BUF) ? PIPE_BUF : bytes_left; | |
114 int bytes_written = write(output_fd_, buffer, bytes_to_write); | |
palmer
2013/04/02 18:01:08
write(2) returns ssize_t, not int.
| |
115 if (bytes_written != bytes_to_write) | |
palmer
2013/04/02 18:01:08
You detect short writes, but not errors (return of
| |
116 VLOG(0) << "Only wrote " << bytes_written | |
117 << " bytes but expected " << bytes_to_write; | |
118 bytes_left -= bytes_written; | |
119 buffer += bytes_written / sizeof(decoded_buffer[0]); | |
palmer
2013/04/02 18:01:08
You could just use sizeof(buffer[0]) or sizeof(int
| |
120 } | |
121 } | |
122 | |
123 bool WebAudioMediaCodecBridge::RegisterWebAudioMediaCodecBridge(JNIEnv* env) { | |
124 bool ret = RegisterNativesImpl(env); | |
125 return ret; | |
126 } | |
127 | |
128 } // namespace | |
OLD | NEW |