Android audio and video development practice series-02-CMake compile rtmpdump

Table of contents

I. Introduction

Two, clone rtmpdump source code

3. Create a new Native C++ project

4. Copy librtmp source code to the project

5. Add CMake build script for librtmp

6. Include the librtmp CMake project as a build dependency

7. Specify the librtmp library header file directory

Eight, associated librtmp library

9. Modify the native-lib.cpp code to print the version number of rtmp

References


I. Introduction

In the previous Android audio and video development practice series-01-ndk-build compiling rtmpdump article explained the use of ndk-build and MakeFile to compile the Rtmpdump library under the Linux platform, but sometimes we do not always have the right conditions to do so, for example we The Windows system used does not support MakeFile. At this time, CMake is very suitable, and the code volume of the Rtmpdump library is not much, and the compilation pressure is not great. However, for x264, faac, ffmepg and other libraries, it is recommended to perform cross-compilation and comparison on the Linux platform. suitable.

Two, clone rtmpdump source code

rtmpdump official website: http://rtmpdump.mplayerhq.hu/

git clone git://git.ffmpeg.org/rtmpdump

As usual, first clone the source code.

3. Create a new Native C++ project

Create a new native c++ project and use CMake to build JNI by default.

4. Copy librtmp source code to the project

Open the rtmpdump source directory of the clone.

Copy the librtmp directory to the /src/main/cpp directory in the project directory:

5. Add CMake build script for librtmp

Copy CMakeLists.txt in the cpp directory to the librtmp directory:

Modify the content of CMakeLists.txt in librtmp as follows:

#配置C预编译宏
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_CRYPTO" )

#所有c源文件放入 librtmp 变量
file(GLOB librtmp *.c)

#编译静态库
add_library(rtmp STATIC ${librtmp} )

CMAKE_C_FLAGS : Options when compiling C files, such as -g; you can also add compilation options through add_definitions. Since the librtmp library is written entirely in C, this variable is used.

If CPP files are included, CMAKE_CXX_FLAGS should be used .

And -DNO_CRYPTO is a compiled macro, which is used to configure the librtmp library not to use the encryption library. For details, see the article mentioned at the beginning.

file : file operation command. The GLOB option will generate a list of files for all files matching the query expression and store the list in variable. The librtmp here is the variable that stores the file, and the query expression uses *.c to match all c files.

add_library : A command to add a source code file or library. Here, the name of the library is set to rtmp, and the target_link_libraries command will be used to pass in the name of the rtmp library when linking the library later . Use STATIC to set the rtmp library to be compiled as a static library (.a/.lib), and use SHARED if it needs to be compiled into a dynamic library (.so/.dll) . Why is it compiled into a static library here? Because from the perspective of function library integration, if you want to integrate all the released sub-libraries (more than one) into a dynamic library to provide an interface to the outside, then you need to compile all sub-libraries into static libraries, so that all sub-libraries can be fully compiled Into the target dynamic library, the final dynamic library that integrates all sub-libraries provides external functions. Finally, provide the relative path of the source code, because the path of all the c source files of the rmtp library has been stored in the librtmp variable using file, so you can use it directly with ${librtmp} .

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add_library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
             rtmp

             # Sets the library as a shared library.
             STATIC

             # Provides a relative path to your source file(s).
             ${librtmp}  )

Extended reading: The difference between static libraries and dynamic libraries

6. Include the librtmp CMake project as a build dependency

The librtmp directory in the cpp directory has its own CMakeLists.txt and is also a CMake project. Therefore, we need to use the add_subdirectory() command in the CMakeLists.txt in the cpp directory (the top-level CMake build script, the CMakeLists.txt file configured in build.gradle , as shown in the following code) to specify the CMakeLists.txt file in the librtmp directory For build dependencies, the build output of librtmp will be included when compiling.

externalNativeBuild {
    cmake {
        path file('src/main/cpp/CMakeLists.txt')
        version '3.10.2'
    }
}

In the CMakeLists.txt file in the cpp directory, add a line:add_subdirectory(librtmp)

# 添加位于librtmp目录下的 CMakeLists.txt 文件
# 作为构建依赖
add_subdirectory(librtmp)
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("rtmpdumpjni")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

# 添加位于librtmp目录下的 CMakeLists.txt 文件
# 作为构建依赖
add_subdirectory(librtmp)

add_library( # Sets the name of the library.
        rtmpdumpjni

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        rtmpdumpjni

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

7. Specify the librtmp library header file directory

In order for CMake to find the header files at compile time, we also need to add the include_directories() command to the CMake build script and specify the path to the header files:

#指定librtmp头文件查找路径
include_directories(librtmp)
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("rtmpdumpjni")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

# 添加位于librtmp目录下的 CMakeLists.txt 文件
# 作为构建依赖
add_subdirectory(librtmp)

#指定librtmp头文件查找路径
include_directories(librtmp)

add_library( # Sets the name of the library.
        rtmpdumpjni

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        rtmpdumpjni

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

Eight, associated librtmp library

In order to allow the generated library to call the functions in the librtmp library, we also need to use the target_link_libraries() command in the CMake build script to associate the librtmp library, find the target_link_libraries command in the CMakeLists.txt file in the cpp directory, and modify it as follows:

target_link_libraries( # Specifies the target library.
        rtmpdumpjni

        #编译并关联rtmp库到rtmpdumpjni库
        rtmp

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

9. Modify the native-lib.cpp code to print the version number of rtmp

Modify the native-lib.cpp file in the cpp directory, refer to the rtmp.h header file in the librtmp directory, and call RTMP_LibVersion to get the version number and return it.

#include <jni.h>
#include <string>
#include "librtmp/rtmp.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_nxg_rtmpdumpjni_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    char version[100];
    sprintf(version, "rtmp version : %d", RTMP_LibVersion());
    return env->NewStringUTF(version);
}

Finally compile and run as follows:

Successfully display the version number of the rtmp library. So far, the Rtmpdump library has been compiled.

References

Configure CMake

Guess you like

Origin blog.csdn.net/xiangang12202/article/details/122186092
Recommended