preface

Here is an apology, because I am still a junior, I have a lot of classes. There will be a serious disconnect in the update progress due to the recent five compulsory courses. However, I will try to guarantee an article every week. I hope you can understand.

Article Content Bibliography “Audio and Video Development Advanced Guide — Based on Android and iOS platform practice”

Making portal

directory

[1] [From Zero impact audio and video development] Basic knowledge of audio and video development

[2] [Zero-impact audio and video Development] Mobile environment construction

[3] [From Zero impact audio and video development] FFmpeg introduction and basic use

Mobile Environment construction (taking AS AS example)

In fact, a lot of websites have had such an introduction, but since it is their own article, or need to talk about, hey hey. Create a new AS project and select the option to go to our Native C++ to create our project. Of course we’ll see a C++ standard inside, and I’ll default to Toolchain Defalut.

At this point our program is actually ready to run, you can just run it, but if you look at the code, you’ll see that there are some differences

Why do we use itNative C++?

Java is not a universal language after all, and every language has its own strengths and limitations. Why else would you need a Binder mechanism to call some of the underlying services, because there are a lot of things that the upper layer can’t do.

Cases can be divided into the following:

(1) The application needs some platform-related features, and the Java layer does not provide the corresponding API (such as the use of OpenSL ES).

(2) Call the existing function library written in C++.

(3) The application has certain processing speed requirements for some key operations.

How to callNativecode

(1) Write a tapenativeA function of the method.

public class NativeTest {
    public native void encode(a);
}
Copy the code

Create a file native, but you may see this function become popular. It doesn’t matter, let’s Build it with a hammer. Normally, it won’t get an error, because the native function doesn’t find the corresponding C++ function. It doesn’t affect our own Java classes.

(2) Generate the one we needC\C++The file.

Switch to the SRC /main/ Java directory. 2. In Terminal, enter the javah-jni package name. Java classes with native / / in my project statement javah - jni com. Clericyi. Player. NativeTest 3. Get a header file of the format :(package name _ class name).h and move it to the CPP directoryCopy the code

(3) The resulting.h header file can not be used, naturally need.cpp file to support the use of. (The location is given in the figure above)

#include <jni.h>
#include <string.h>
#include "com_clericyi_player_NativeTest.h"

JNIEXPORT jstring JNICALL Java_com_clericyi_player_NativeTest_encode
        (JNIEnv *env, jobject) {
    return env->NewStringUTF("Encoder encoding");
}
Copy the code

(4) Use

In fact, this time still can not use, need to carry out certain configuration, go through the following process should be able to see our operation effect.

(1) modify the corresponding data in cmakelist. TXT, add CPP file add_library(#...
        audioencoder.cpp)

(2) app -> build.gradle
android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                cFlags "-DSTDC_HEADERS"}} NDK {// we can add x86, etc., according to the phone architecture of the application // write a few, then the build will generate several abiFilters"armeabi-v7a"}}}Copy the code

About NDK installation

Because we have to use NDK in the development process, and NDK version compatibility is a very important thing. In order to be compatible with some versions downward, we can choose the lower version. (Download path: Prefereces -> Android SDK -> SDK Tools)

I believe many people will find that others install NDK while I install NDK (Side by Side), but you can’t find the NDK we need on your AS either. So here is a website for readers to use as an installation tutorial: Installing and configuring NDK and CMake

rendering

Principle and practice of cross compilation

What is cross-compilation?

Three parts:

  1. Source code from a third party
  2. The operating equipment isAndroidoriOS
  3. The compilation machine is our PC

To put it simply, our localPCCompilation produces a copy of something that someone else can use.

Cross-compilation practices

Use LAME as an example to make a detailed demonstration.

The book method is no longer recommended here, I’ve been trying it all day, and it’s too difficult. Again, use the solution that Android Studio now automatically generates with it — CMake.

  1. Finish downloading oursLAME,libmp3lameUnder the documents andincludeUnder thelame.hFiles are all moved toASIn thecpp\lameUnder the folder of.

  1. Modify theCMakeLists.txt
Add_library (# Sets the name of the library. # Sets the name of the library Provides a relative path to your source file(s). # CPP file native-lib. CPP # lame lame/bitstream.c lame/encoder lame/gain_analysis.c lame/lame.c lame/id3tag.c lame/mpglib_interface.c lame/newmdct.c lame/presets.c lame/psymodel.c lame/quantize.c lame/fft.c lame/quantize_pvt.c lame/reservoir.c lame/set_get.c lame/tables.c lame/takehiro.c lame/util.c  lame/vbrquantize.c lame/VbrTag.c lame/version.c)Copy the code
  1. With a small hammerbuildFollowing our project, an error will be reported. The error I made is given below the solution.
Error 1:#include <lame.h>Solution:#include "lame.h"-- Error 2: IEEE754_FLOAT32_t fast_log2(IEEE754_FLOAT32_t x); Solution:float fast_log2(float x);
Copy the code
  1. After correcting the error, we can start our use.
#include <jni.h>
#include <string.h>
#include "com_clericyi_player_NativeTest.h"

JNIEXPORT jstring JNICALL Java_com_clericyi_player_NativeTest_encode
        (JNIEnv *env, jobject) {
    return env->NewStringUTF(get_lame_version());
}
Copy the code

You can change the return value of the C++ file you used above to this to get the effect.

That’s basically it. If you want to take a deeper look, you can look at the structure of the project for self-study. The content in the corresponding project is an example from the book for converting PCM files to MP3 format.

PCM files can be picked up by themselves, because I used ADB to push them directly to the virtual machine for convenience.

The above is my learning results, if there is anything I did not think about or there is a mistake in the article, welcome to share with me.


Related articles recommended:

The interviewer asked me, “What is generic erasure and what problems does it cause?”

【 Audio and video development from Zero impact 】 The basic knowledge of audio and video development

Promise me, keep this dingding and Douyin profile, it’s really important!!