FFMPEG+OPENSLES+生产者模式播放音视频(一)

先讲一下编译FFMPEG及其配置

  1. 下载:wget http://ffmpeg.org/releases/ffmpeg-2.6.9.tar.gz
  2. 解压缩: tar -xzf ffmpeg-2.6.9.tar.gz
  3. 赋予权限:chmod 777 -R ffmpeg-2.6.9
  4. 修改configure文件中的2775行开始的4行代码
    改之前:
    SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
    LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
    SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
    SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR)$(SLIBNAME)'
    
    改之后:
    SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
    LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
    SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
    SLIB_INSTALL_LINKS='$(SLIBNAME)'
    
  5. 编写build_android.sh
    #!/bin/bash
    make clean
    export NDK=/ndk/android-ndk-r15
    export SYSROOT=$NDK/platforms/android-9/arch-arm/
    export TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
    export CPU=arm
    export PREFIX=$(pwd)/android/$CPU
    export ADDI_CFLAGS="-marm"
    
    ./configure --target-os=linux \
    --prefix=$PREFIX --arch=arm \
    --disable-doc \
    --enable-shared \
    --disable-static \
    --disable-yasm \
    --disable-symver \
    --enable-gpl \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-ffserver \
    --disable-doc \
    --disable-symver \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -fpic $ADDI_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    $ADDITIONAL_CONFIGURE_FLAG
    make clean
    make
    make install
    
  6. 赋予权限:chmod 777 build_android.sh
  7. 运行:./build_android.sh
  8. 新建android studio工程,将生成的android/arm/lib中的8个so文件,拷贝到libs\armeabi:
    libavcodec-56.so
    libavdevice-56.so
    libavformat-56.so
    libavutil-54.so
    libpostproc-53.so
    libswresample-1.so
    libswscale-3.so
    
  9. 在moudle 下的defaultConfig节点加入下面代码
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    externalNativeBuild {
        cmake {
            cppFlags "-frtti -fexceptions"
            abiFilters 'armeabi'
        }
    }
    
  10. 将生成的android/arm/include整个文件夹,拷贝到as工程中的libs文件夹里面。
  11. 修改CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib
             SHARED
             src/main/cpp/native-lib.cpp )
find_library( log-lib
              log )
include_directories(libs/include)
set(DIR ../../../../libs)
add_library(avcodec-56
            SHARED
            IMPORTED)
set_target_properties(avcodec-56
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavcodec-56.so)
add_library(avdevice-56
            SHARED
            IMPORTED)
set_target_properties(avdevice-56
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavdevice-56.so)
add_library(avformat-56
            SHARED
            IMPORTED)
set_target_properties(avformat-56
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavformat-56.so)
add_library(avutil-54
            SHARED
            IMPORTED)
set_target_properties(avutil-54
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavutil-54.so)
add_library(postproc-53
            SHARED
            IMPORTED)
set_target_properties(postproc-53
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libpostproc-53.so)
add_library(swresample-1
            SHARED
            IMPORTED)
set_target_properties(swresample-1
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libswresample-1.so)
add_library(swscale-3
            SHARED
            IMPORTED)
set_target_properties(swscale-3
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libswscale-3.so)
add_library(avfilter-5
            SHARED
            IMPORTED)
set_target_properties(avfilter-5
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavfilter-5.so)
target_link_libraries( native-lib
                       avcodec-56
                       avdevice-56
                       avformat-56
                       avutil-54
                       postproc-53
                       swresample-1
                       swscale-3
                       avfilter-5
                       ${log-lib} )

12.编译运行,如没出错,则说明编译配置正确

项目地址:FFmpegDemo

你可能感兴趣的:(FFMPEG+OPENSLES+生产者模式播放音视频(一))