Code building for Android ilbc voice conversation demonstration

Source: Internet
Author: User

Based on a copy of the code (http://code.google.com/p/android-ilbc/) of the google site mentioned in the previous article, this requires git download, I uploaded a copy in CSDN,
Slightly modified: Download link :( http://download.csdn.net/detail/ranxiedao/4450917 ).
Now we will explain how to build the code structure:
Requirements:
Environment: Ubuntu 12.04 (available in other Linux environments), Android 2.2 and later
 
Tool: Elicpse 3.7, Android NDK r7, Android SDK r7

1. Create a project:
Open Eclipse and create an Android program named AndroidILBC. the SDK Version 2.2 and later is required because Android officially supports NDK development from 2.2.

2. Add the underlying code:
Copy the jni folder in the downloaded source code to the root directory of the new project. The code structure is as follows:

3. The content of Android. mk in the jni directory is:
LOCAL_PATH: = $ (call my-dir)

Include $ (CLEAR_VARS)

LOCAL_MODULE: = libilbc

Codec_dir: = iLBC_RFC3951 # ilbc source code directory

LOCAL_SRC_FILES: = \
$ (Codec_dir)/anaFilter. c \
$ (Codec_dir)/constants. c \
$ (Codec_dir)/createCB. c \
$ (Codec_dir)/doCPLC. c \
$ (Codec_dir)/enhancer. c \
$ (Codec_dir)/filter. c \
$ (Codec_dir)/FrameClassify. c \
$ (Codec_dir)/gainquant. c \
$ (Codec_dir)/getCBvec. c \
$ (Codec_dir)/helpfun. c \
$ (Codec_dir)/hpInput. c \
$ (Codec_dir)/hpOutput. c \
$ (Codec_dir)/iCBConstruct. c \
$ (Codec_dir)/iCBSearch. c \
$ (Codec_dir)/iLBC_decode.c \
$ (Codec_dir)/iLBC_encode.c \
$ (Codec_dir)/LPCdecode. c \
$ (Codec_dir)/LPCencode. c \
$ (Codec_dir)/lsf. c \
$ (Codec_dir)/packing. c \
$ (Codec_dir)/StateConstructW. c \
$ (Codec_dir)/StateSearchW. c \
$ (Codec_dir)/syntFilter. c

Local_c_shortdes + = $ (common_c_shortdes)

LOCAL_PRELINK_MODULE: = false

Include $ (BUILD_STATIC_LIBRARY)

# Build JNI wrapper
Include $ (CLEAR_VARS)

LOCAL_MODULE: = libilbc-codec # generated. so library name, which can be modified by yourself
LOCAL_C_INCLUDES + = \
$ (JNI_H_INCLUDE )\
$ (Codec_dir)

LOCAL_SRC_FILES: = ilbc-codec.c
LOCAL_LDLIBS: =-L $ (SYSROOT)/usr/lib-llog

LOCAL_STATIC_LIBRARIES: = libilbc
LOCAL_PRELINK_MODULE: = false

Include $ (BUILD_SHARED_LIBRARY)

4. Code Analysis:
Open the ilbc-codec.c file under the jni folder, which has only five functions in total, responsible for audio codec initialization, and audio encoding and decoding. The three methods are as follows:
Jint Java_com_googlecode_androidilbc_Codec_init (
JNIEnv * env, jobject this, jint mode)
And
 
Jint Java_com_googlecode_androidilbc_Codec_encode (
JNIEnv * env, jobject this,
JbyteArray sampleArray, jint sampleOffset, jint sampleLength,
JbyteArray dataArray, jint dataOffset)
And
Jint Java_com_googlecode_androidilbc_Codec_decode (
JNIEnv * env, jobject this,
JbyteArray dataArray, jint dataOffset, jint dataLength,
JbyteArray sampleArray, jint sampleOffset)
According to the names of the three functions, we can know that the three functions used to call the Java-Layer Code are now transformed (just change the function name)

5. Write the native method at the Java layer:
In the Java-Layer Code of the program, create a package for storing the java-Layer Code of NDK. For example, create a package named xmu. swordbearer. audio and create a class:
AudioCodec. java is only responsible for calling the underlying C function in this class, which is equivalent to a tool class. Create three new public staic native int methods:
Package xmu. swordbearer. audio;
 
Public class AudioCodec {
// Initialize decoder and encoder
Public static native int audio_codec_init (int mode );
 
// Encode
Public static native int audio_encode (byte [] sample, int sampleOffset,
Int sampleLength, byte [] data, int dataOffset );
 
// Decode
Public static native int audio_decode (byte [] data, int dataOffset,
Int dataLength, byte [] sample, int sampleLength );
}

The three methods are used for initialization, audio encoding, and audio decoding. Here, you only need to declare them as native methods without writing any code;
6. Compile the. h header file: (if not, refer to the previous article)
Open the terminal and go to the AudioCodec. java directory created in step 1, as shown below:

This step is critical. after entering the src directory, you must include the package name of the AudioCodec class. In this example, the package name is xmu. swordbearer. audio.
If the preceding statement is correct, an xmu_swordbearer_audio_AudioCodec.h header file is generated under the package. The content of the header file is as follows:
/*
* Class: xmu_swordbearer_audio_AudioCodec
* Method: audio_codec_init
* Signature: (I) I
*/
JNIEXPORT jint JNICALL Java_xmu_swordbearer_audio_AudioCodec_audio_1codec_1init
(JNIEnv *, jclass, jint );
 
/*
* Class: xmu_swordbearer_audio_AudioCodec
* Method: audio_encode
* Signature: ([BII [BI) I
*/
JNIEXPORT jint JNICALL Java_xmu_swordbearer_audio_AudioCodec_audio_1encode
(JNIEnv *, jclass, jbyteArray, jint, jint, jbyteArray, jint );
 
/*
* Class: xmu_swordbearer_audio_AudioCodec
* Method: audio_decode
* Signature: ([BII [BI) I
*/
JNIEXPORT jint JNICALL Java_xmu_swordbearer_audio_AudioCodec_audio_1decode
(JNIEnv *, jclass, jbyteArray, jint, jint, jbyteArray, jint );
In step 2, the three method modifications analyzed, open the ilbc-codec.c file under jni, replace the three names with the three method names that have just been generated, the specific correspondence is as follows:
Java_com_googlecode_androidilbc_Codec_init
Changed:
Java_xmu_swordbearer_audio_AudioCodec_audio_1codec_1init
 
Java_com_googlecode_androidilbc_Codec_encode
Changed:
Java_xmu_swordbearer_audio_AudioCodec_audio_1encode
 
Java_com_googlecode_androidilbc_Codec_decode
Changed:
Java_xmu_swordbearer_audio_AudioCodec_audio_1decode
It is only a copy and paste process !!!
Of course, if the package name or method name of the JAVA code you write is different, the methods in the generated. h file are different. That is why the. so library cannot be compiled
Modify the package name of the class where the native method is located, because the method name will change.

7. Compile the. so Library
Below is to compile the generated. so library, as written in the above Android. mk file, the final compilation generated library is libilbc-codec.so, the compilation method is as follows:
Open the terminal, go to the jni folder, enter ndk-build, and press Enter. The following figure is displayed:

Do you see the second to last line? Libs/armeabi/libilbc-codec.so, indicating that the dynamic library we need has been generated, then you will find that there is an additional libs under the root directory of the project
Folder, which has an armeabi directory, opened with a libilbc-codec.so file:


After obtaining this library, we have completed all the work related to the underlying layer. If we are abused by Linux, we can immediately switch to Windows, and the subsequent work will no longer need to be carried out in Liunx.
OK. The library compilation is complete. We will demonstrate the audio collection and how to use Java to call the underlying codec function in the future.

8. Summary:
I have studied the compilation process for two days. As mentioned in the previous article, I started to use the demo provided by my classmates. Although it can be run, the program's scalability is not good and you cannot
After you can write a program, there is also a strange package name in it, and you can't even touch this package name, plus the nesting of various code, summed up a sentence-do it yourself, good clothes and food!
After several studies, I have accumulated some experience in writing Android. mk files, and the NDK compilation process has been easy. Now I can modify my code as needed.
You can directly compile the code and re-compile a. so library, or even use this library in other programs.
Android NDK does provide a very good platform. From the FFMPEG porting during video processing to the current ILBC library porting, all C code will be used.
Game Development may have a broader world.
It took three hours to write this article, because I just learned to write code and read the ilbc source code again. The actual code of ilbc was several times from start to end.
Thousands of lines, not much, but excellent. It takes some time to master. At present, this series of tutorials only completed the underlying development. Next, we will improve the entire system. Some of the articles are not well written.
Hope you can give me more advice and learn from each other!

--------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.