Android知识WebRTCWebRTC

android噪音抑制--Webrtc NS模块编译

2017-03-29  本文已影响754人  ncd
package com.nativetools;
public class WebrtcNS {
    static{ 
        System.loadLibrary("WebrtcNS");  //加载native code的动态库
    } 
    public native int createNSInstance();
    public native int initNSInstance(int nsHandler,long sf);
    public native int setNSPolicy(int nsHandler,int mode);
    public native void nsProcess(int nsHandler,short bufferIn[],short bufferIn_H[],short bufferOut[],short bufferOut_H[]);
    public native void nsFree(int nsHandler);
}

然后打开cmd,进入此工程的bin->classes目录,输入javah com.nativetools.WebrtcNS就可以在bin->classes文件夹下得到头文件com_nativetools_WebrtcNS.h:
<pre>/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_nativetools_WebrtcNS */
#ifndef _Included_com_nativetools_WebrtcNS
#define _Included_com_nativetools_WebrtcNS
#ifdef __cplusplus
extern "C" {
#endif</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: createNSInstance
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_nativetools_WebrtcNS_createNSInstance
(JNIEnv *, jobject);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: initNSInstance
* Signature: (IJ)I
*/
JNIEXPORT jint JNICALL Java_com_nativetools_WebrtcNS_initNSInstance
(JNIEnv *, jobject, jint, jlong);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: setNSPolicy
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_nativetools_WebrtcNS_setNSPolicy
(JNIEnv *, jobject, jint, jint);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: nsProcess
* Signature: (I[S[S[S[S)V
*/
JNIEXPORT void JNICALL Java_com_nativetools_WebrtcNS_nsProcess
(JNIEnv *, jobject, jint, jshortArray, jshortArray, jshortArray, jshortArray);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: nsFree
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_nativetools_WebrtcNS_nsFree
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
</pre>
将此头文件复制到jni中备用。
在jni下新建demo.cpp,引入com_nativetools_WebrtcNS.h,编写jni:
<pre>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include "ns/include/com_nativetools_WebrtcNS.h"
#include "ns/include/noise_suppression.h"
#include "ns/include/ns_core.h"
extern "C" {
JNIEXPORT jint JNICALL
Java_com_nativetools_WebrtcNS_createNSInstance(JNIEnv *env, jobject jobj) {
NsHandle *hNS = NULL; //create a pointer to NsHandle on native stack.
if (WebRtcNs_Create(&hNS) == -1) { //allocate dynamic memory on native heap for NS instance pointed by hNS.
return -1;//error occurs
} else {
return ((int) (NSinst_t *) hNS); //returns the address of NS instance on native heap.
}
}

JNIEXPORT jint JNICALL
Java_com_nativetools_WebrtcNS_initNSInstance(JNIEnv \*env, jobject jobj, jint nsHandler, jlong sf) {
NsHandle \*hNS = (NsHandle\*) nsHandler;
return WebRtcNs_Init(hNS, sf);
}

JNIEXPORT jint JNICALL
Java_com_nativetools_WebrtcNS_setNSPolicy(JNIEnv \*env, jobject jobj, jint nsHandler, jint mode){
NsHandle \*hNS = (NsHandle\*) nsHandler;
return WebRtcNs_set_policy(hNS,mode);
}

JNIEXPORT void JNICALL
Java_com_nativetools_WebrtcNS_nsProcess(JNIEnv \*env, jobject jobj, jint nsHandler, jshortArray bufferIn, jshortArray bufferIn_H, jshortArray bufferOut, jshortArray bufferOut_H){
NsHandle \*hNS = (NsHandle*) nsHandler;
jshort *spFrameInData = env->GetShortArrayElements(bufferIn,0);
jshort \*spFrameOutData = env->GetShortArrayElements(bufferOut,0);

short \* spFrame_In = (short \*)spFrameInData;
short \* spFrame_Out = (short \*)spFrameOutData;
WebRtcNs_Process(hNS,spFrame_In,NULL,spFrame_Out,NULL);
(env)->ReleaseShortArrayElements(bufferIn, spFrameInData, 0);
(env)->ReleaseShortArrayElements(bufferOut, spFrameOutData, 0);
}

JNIEXPORT void JNICALL
Java_com_nativetools_WebrtcNS_nsFree(JNIEnv \*env, jobject jobj, jint nsHandler){
NsHandle \*hNS = (NsHandle*) nsHandler;
WebRtcNs_Free(hNS);
}

}
</pre>

上一篇 下一篇

猜你喜欢

热点阅读