OSDN Git Service

Merge "voice processing"
[android-x86/system-media.git] / mca / filterfw / java / android / filterfw / core / NativeProgram.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 package android.filterfw.core;
19
20 import android.filterfw.core.Frame;
21 import android.filterfw.core.Program;
22
23 public class NativeProgram extends Program {
24
25     private int nativeProgramId;
26     private boolean mHasInitFunction     = false;
27     private boolean mHasTeardownFunction = false;
28     private boolean mHasSetValueFunction = false;
29     private boolean mHasGetValueFunction = false;
30
31     public NativeProgram(String nativeLibName, String nativeFunctionPrefix) {
32         // Allocate the native instance
33         allocate();
34
35         // Open the native library
36         String fullLibName = "lib" + nativeLibName + ".so";
37         if (!openNativeLibrary(fullLibName)) {
38             throw new RuntimeException("Could not find native library named '" + fullLibName + "' " +
39                                        "required for native program!");
40         }
41
42         // Bind the native functions
43         String processFuncName = nativeFunctionPrefix + "_process";
44         if (!bindProcessFunction(processFuncName)) {
45             throw new RuntimeException("Could not find native program function name " +
46                                        processFuncName + " in library " + fullLibName + "! " +
47                                        "This function is required!");
48         }
49
50         String initFuncName = nativeFunctionPrefix + "_init";
51         mHasInitFunction = bindInitFunction(initFuncName);
52
53         String teardownFuncName = nativeFunctionPrefix + "_teardown";
54         mHasTeardownFunction = bindTeardownFunction(teardownFuncName);
55
56         String setValueFuncName = nativeFunctionPrefix + "_setvalue";
57         mHasSetValueFunction = bindSetValueFunction(setValueFuncName);
58
59         String getValueFuncName = nativeFunctionPrefix + "_getvalue";
60         mHasGetValueFunction = bindGetValueFunction(getValueFuncName);
61
62         // Initialize the native code
63         if (mHasInitFunction && !callNativeInit()) {
64             throw new RuntimeException("Could not initialize NativeFrame!");
65         }
66     }
67
68     @Override
69     protected void finalize() throws Throwable {
70         if (mHasTeardownFunction && !callNativeTeardown()) {
71             throw new RuntimeException("Could not tear down NativeFrame!");
72         }
73         deallocate();
74     }
75     @Override
76     public void process(Frame[] inputs, Frame output) {
77         NativeFrame[] nativeInputs = new NativeFrame[inputs.length];
78         for (int i = 0; i < inputs.length; ++i) {
79             if (inputs[i] instanceof NativeFrame) {
80                 nativeInputs[i] = (NativeFrame)inputs[i];
81             } else {
82                 throw new RuntimeException("NativeProgram got non-native frame as input "+ i +"!");
83             }
84         }
85
86         // Get the GL output frame
87         NativeFrame nativeOutput = null;
88         if (output instanceof NativeFrame) {
89             nativeOutput = (NativeFrame)output;
90         } else {
91             throw new RuntimeException("NativeProgram got non-native output frame!");
92         }
93
94         // Process!
95         if (!callNativeProcess(nativeInputs, nativeOutput)) {
96             throw new RuntimeException("Calling native process() caused error!");
97         }
98     }
99
100     @Override
101     public void setHostValue(String variableName, Object value) {
102         if (!mHasSetValueFunction) {
103             throw new RuntimeException("Attempting to set native variable, but native code does not " +
104                                        "define native setvalue function!");
105         }
106         if (!callNativeSetValue(variableName, value)) {
107             throw new RuntimeException("Error setting native value for variable '" + variableName + "'!");
108         }
109     }
110
111     @Override
112     public Object getHostValue(String variableName) {
113         if (!mHasGetValueFunction) {
114             throw new RuntimeException("Attempting to get native variable, but native code does not " +
115                                        "define native getvalue function!");
116         }
117         return callNativeGetValue(variableName);
118     }
119
120     static {
121         System.loadLibrary("filterfw");
122     }
123
124     private native boolean allocate();
125
126     private native boolean deallocate();
127
128     private native boolean nativeInit();
129
130     private native boolean openNativeLibrary(String libName);
131
132     private native boolean bindInitFunction(String funcName);
133     private native boolean bindSetValueFunction(String funcName);
134     private native boolean bindGetValueFunction(String funcName);
135     private native boolean bindProcessFunction(String funcName);
136     private native boolean bindTeardownFunction(String funcName);
137
138     private native boolean callNativeInit();
139     private native boolean callNativeSetValue(String key, Object value);
140     private native Object  callNativeGetValue(String key);
141     private native boolean callNativeProcess(NativeFrame[] inputs, NativeFrame output);
142     private native boolean callNativeTeardown();
143 }