美文网首页Android JNI
用cmake对ffmpeg的so库进行调用

用cmake对ffmpeg的so库进行调用

作者: zzl93 | 来源:发表于2019-03-14 16:17 被阅读170次

    本文是在ffmpeg的编译完成的基础上用cmake对ffmpeg的so库进行调用

    具体步骤

    一、创建一个包含c++的项目

    创建新项目的时候,记得勾选include c++ support,项目中会在main目录下自动创建cpp目录并生成一个现成的cpp文件。

    二、将生成的so库和头文件拷贝进项目中

    1、在main目录下创建jnilibs目录并创建对应的abi目录(由于我生成的ffmpeg仅仅支持armeabi和armeabi-v7a,而armeabi又不需要创建,所以此处只创建了armeabi-v7a目录),将ffmpeg的so库复制进去。 image.png 2、在src/main/cpp中拷贝ffmpeg编译后的头文件(直接将include文件夹拷贝进来) image.png

    效果如下:


    image.png

    三、配置CMakeLists.txt 文件(CMake构建脚本)

    具体在CMakeLists中添加的Cmake命令用法,请参考这篇文章
    需要注意的是CMakelists的位置,CMAKE_SOURCE_DIR就代表CMakeLists所在的路径。我的项目中CMakeLists的路径是与build.gradle同级的

    image.png
    下面贴出我的CMakeLists
    # For more information about using CMake with Android Studio, read the
    cmake_minimum_required(VERSION 3.4.1)
    
    set(lib_src_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
    
    message("CURRENT_DIR:" ${CMAKE_SOURCE_DIR})
    ##添加一个库
    add_library( # Sets the name of the library.
            native-lib
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            ${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp)
    add_library(avcodec-57 SHARED IMPORTED)
    add_library(avformat-57 SHARED IMPORTED)
    add_library(avutil-55 SHARED IMPORTED)
    add_library(swresample-2 SHARED IMPORTED)
    add_library(swscale-4 SHARED IMPORTED)
    
    set_target_properties(avcodec-57 PROPERTIES IMPORTED_LOCATION
            ${lib_src_DIR}/libavcodec-57.so)
    set_target_properties(avformat-57 PROPERTIES IMPORTED_LOCATION
            ${lib_src_DIR}/libavformat-57.so)
    set_target_properties(avutil-55 PROPERTIES IMPORTED_LOCATION
            ${lib_src_DIR}/libavutil-55.so)
    set_target_properties(swresample-2 PROPERTIES IMPORTED_LOCATION
            ${lib_src_DIR}/libswresample-2.so)
    set_target_properties(swscale-4 PROPERTIES IMPORTED_LOCATION
            ${lib_src_DIR}/libswscale-4.so)
    
    include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)
    #找到log库
    find_library( # Sets the name of the path variable.
            log-lib
    
            log)
    ##给目标库添加依赖库
    target_link_libraries(
            native-lib#目标库
    
            # 依赖库,可以写多个
            ${log-lib}
            avcodec-57
            avformat-57
            avutil-55
            swresample-2
            swscale-4)
    

    Androidstudio最新版好像创建完c++后自动生成的CMakeLists是在cpp目录下的,这个CMakeLists在哪都行,不过千万要记住修改几处调用CMakeLists路径的地方。有以下几处:
    1、CMakeLists中用到CMAKE_SOURCE_DIR的地方


    image.png

    2、build.gradle中


    image.png

    四、编写 C++代码

    include/libavcodec/avcodec.h是引用的so库的头文件,需要注意使用extern "C"包裹起来

    #include <jni.h>
    #include <string>
    
    extern "C" {
    #include "include/libavcodec/avcodec.h"
    }
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_face_jfshare_nativetest_MainActivity_stringFromJNI(
            JNIEnv *env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    extern "C"
    JNIEXPORT jstring JNICALL
    Java_com_face_jfshare_nativetest_MainActivity_stringTest(JNIEnv *env,
                                                             jobject /* this */) {
        char info[10000] = {0};
        sprintf(info, "%s\n", avcodec_configuration());
        return env->NewStringUTF(info);
    }
    

    五、调用本地代码

    public class MainActivity extends AppCompatActivity {
    
        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("native-lib");
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Example of a call to a native method
            TextView tv = findViewById(R.id.sample_text);
            tv.setText(stringTest());
        }
    
        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        public native String stringFromJNI();
        public native String stringTest();
    }
    

    六、运行app

    56895DA6D4025A63FAF295CED96F3C2F.jpg

    注意事项

    1:由于我生成的ffmpeg仅仅支持armeabi和armeabi-v7a所以在build.gradle中需要注意只能匹配armeabi-v7a(高版本ndk不允许匹配armeabi)

        ndk{
                abiFilters "armeabi-v7a"
            }
    

    参考链接:
    1、https://www.cnblogs.com/fnlingnzb-learner/p/7593488.html
    2、https://www.jianshu.com/p/a1fe55d5683b

    相关文章

      网友评论

        本文标题:用cmake对ffmpeg的so库进行调用

        本文链接:https://www.haomeiwen.com/subject/zlqcmqtx.html