Android 音视频开发实践系列-02-CMake编译rtmpdump

Android 音视频开发实践系列-02-CMake编译rtmpdump,第1张

Android 音视频开发实践系列-02-CMake编译rtmpdump

目录

一、前言

二、克隆rtmpdump源码

三、新建一个Native C++工程

四、复制librtmp源码到工程中

五、为librtmp增加CMake构建脚本

六、包含librtmp CMake项目作为构建依赖

七、 指定librtmp库头文件目录

八、 关联librtmp库

九、修改native-lib.cpp代码打印rtmp的版本号

参考资料


一、前言

在上一篇Android 音视频开发实践系列-01-ndk-build编译rtmpdump文章讲解了Linux平台下使用ndk-build和MakeFile编译Rtmpdump库,但有时候我们并不总是有合适条件这么干,比如我们用的Window系统不支持MakeFile,这时候CMake就很合适了,而且Rtmpdump库的代码量不多,编译起来压力不大,不过对于x264,faac,ffmepg等库还是建议在Linux平台下进行交叉编译比较合适 。

二、克隆rtmpdump源码

rtmpdump官网:http://rtmpdump.mplayerhq.hu/

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

老规矩,先clone源码。

三、新建一个Native C++工程

新建一个native c++工程默认使用CMake构建JNI。

四、复制librtmp源码到工程中

打开clone的rtmpdump源码目录。

复制librtmp目录到工程目录中的/src/main/cpp目录中:

五、为librtmp增加CMake构建脚本

复制cpp目录中CMakeLists.txt到librtmp目录中:

修改librtmp中的CMakeLists.txt内容如下:

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

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

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

CMAKE_C_FLAGS:编译C文件时的选项,如-g;也可以通过add_definitions添加编译选项。由于librtmp库是全部由C语言编写的,因此使用此变量。

若包含CPP文件,应使用CMAKE_CXX_FLAGS。

而-DNO_CRYPTO是编译宏,用于配置librtmp库不使用加密库,详情见开头提到的文章。

file:文件 *** 作命令。GLOB选项将会为所有匹配查询表达式的文件生成一个文件list,并将该list存储进变量variable里。这里的librtmp就是存储文件的变量,查询表达式使用*.c匹配所有的c文件。

add_library:添加源代码文件或库的命令。这里设置库的名称为rtmp,后面关联该库的时候会使用target_link_libraries命令传入rtmp库的名称。使用STATIC设置rtmp库编译为静态库(.a/.lib),如果需要编译成动态库(.so/.dll)则使用SHARED。为什么这里是要编译成静态库?因为从函数库集成的角度,如果要将发布的所有子库(不止一个)集成为一个动态库向外提供接口,那么就需要将所有子库编译为静态库,这样所有子库就可以全部编译进目标动态库中,由最终的一个集成所有子库的动态库对外提供功能。最后还要提供源码的相对路径,因为已经使用file存储了rmtp库所有的c源文件的路径到librtmp变量中,因此直接${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}  )

扩展阅读:静态库和动态库的区别

六、包含librtmp CMake项目作为构建依赖

cpp目录中的librtmp目录拥有自己的CMakeLists.txt,也是一个CMake项目。因此我们需要在cpp目录中的 CMakeLists.txt(顶级CMake构建脚本,在build.gradle中进行配置的 CMakeLists.txt文件,如下代码所示)中使用 add_subdirectory() 命令将librtmp目录的 CMakeLists.txt 文件指定为构建依赖项,这样编译的时候就会包含librtmp的构建输出。

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

在cpp目录中的 CMakeLists.txt文件中,添加一行: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})
七、 指定librtmp库头文件目录

为了让 CMake 能够在编译时找到头文件,我们还需要向 CMake 构建脚本添加 include_directories() 命令,并指定头文件的路径:

#指定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})
八、 关联librtmp库

为了让构建生成的库能够调用 librtmp 库中的函数,我们还需要使用 CMake 构建脚本中的 target_link_libraries() 命令来关联librtmp库,找到cpp目录中的 CMakeLists.txt文件中的target_link_libraries命令,修改如下:

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})
九、修改native-lib.cpp代码打印rtmp的版本号

修改cpp目录下的native-lib.cpp文件,引用librtmp目录的rtmp.h头文件,并调用RTMP_LibVersion得到版本号返回。

#include 
#include 
#include "librtmp/rtmp.h"

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

最后编译运行如下:

成功显示rtmp库的版本号。至此,Rtmpdump库编译完成。

参考资料

配置 CMake

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5682065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存