title: IJKPlayer Android編譯踩坑
tags: 新建,模板,小書匠
grammar_cjkRuby: true
1. 編譯前準(zhǔn)備
-
編譯環(huán)境說明
- Mac OS X 10.11.5
- Android
- android-ndk-r13b
環(huán)境配置:
編輯~/.bash_profile
瓣俯,添加以下環(huán)境變量:
export ANDROID_SDK=<your sdk path>
export ANDROID_NDK=<your ndk path>
- IJKPlayer相關(guān)源碼下載:
git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-android
cd ijkplayer-android
git checkout -B latest k0.8.8
./init-android.sh
下載完成后雷猪,目錄文件夾如下所示:
2. 編譯
2.1編譯x264
因?yàn)橛玫絰264編碼航邢,所以需要首先編譯出x264庫進(jìn)行使用。
2.1.1.進(jìn)入ijkplayer主目錄庸推,新建init-android-x264.sh文件
腳本如下:
IJK_x264_UPSTREAM=http://git.videolan.org/git/x264.git
IJK_x264_FORK=http://git.videolan.org/git/x264.git
IJK_x264_LOCAL_REPO=extra/x264
set -e
TOOLS=tools
echo "== pull x264 base =="
sh $TOOLS/pull-repo-base.sh $IJK_x264_UPSTREAM $IJK_x264_LOCAL_REPO
function pull_fork()
{
echo "== pull x264 fork $1 =="
sh $TOOLS/pull-repo-ref.sh $IJK_x264_FORK android/contrib/x264-$1 ${IJK_x264_LOCAL_REPO}
}
pull_fork "armv5"
pull_fork "armv7a"
pull_fork "arm64"
pull_fork "x86"
pull_fork "x86_64"
運(yùn)行腳本,在extra目錄下多了一個(gè)x264目錄刊苍。
在android/contrib下面會(huì)生成x264-armv7a等五個(gè)文件夾
2.1.2 在android/contrib下新建compile-x264.sh 和 tools/do-compile-x264.sh
compile-x264.sh腳本如下:
#! /usr/bin/env bash
#
# Copyright (C) 2013-2014 Zhang Rui <bbcallen@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#----------
UNI_BUILD_ROOT=`pwd`
FF_TARGET=$1
set -e
set +x
FF_ACT_ARCHS_32="armv5 armv7a x86"
FF_ACT_ARCHS_64="armv5 armv7a arm64 x86 x86_64"
FF_ACT_ARCHS_ALL=$FF_ACT_ARCHS_64
echo_archs() {
echo "===================="
echo "[*] check archs"
echo "===================="
echo "FF_ALL_ARCHS = $FF_ACT_ARCHS_ALL"
echo "FF_ACT_ARCHS = $*"
echo ""
}
echo_usage() {
echo "Usage:"
echo " compile-x264.sh armv5|armv7a|arm64|x86|x86_64"
echo " compile-x264.sh all|all32"
echo " compile-x264.sh all64"
echo " compile-x264.sh clean"
echo " compile-x264.sh check"
exit 1
}
echo_nextstep_help() {
#----------
echo ""
echo "--------------------"
echo "[*] Finished"
echo "--------------------"
echo "# to continue to build ffmpeg, run script below,"
echo "sh compile-ffmpeg.sh "
echo "# to continue to build ijkplayer, run script below,"
echo "sh compile-ijk.sh "
}
#----------
case "$FF_TARGET" in
"")
echo_archs armv7a
sh tools/do-compile-x264.sh armv7a
;;
armv5|armv7a|arm64|x86|x86_64)
echo_archs $FF_TARGET
sh tools/do-compile-x264.sh $FF_TARGET
echo_nextstep_help
;;
all32)
echo_archs $FF_ACT_ARCHS_32
for ARCH in $FF_ACT_ARCHS_32
do
sh tools/do-compile-x264.sh $ARCH
done
echo_nextstep_help
;;
all|all64)
echo_archs $FF_ACT_ARCHS_64
for ARCH in $FF_ACT_ARCHS_64
do
sh tools/do-compile-x264.sh $ARCH
done
echo_nextstep_help
;;
clean)
echo_archs FF_ACT_ARCHS_64
for ARCH in $FF_ACT_ARCHS_ALL
do
if [ -d x264-$ARCH ]; then
cd x264-$ARCH && git clean -xdf && cd -
fi
done
rm -rf ./build/x264-*
;;
check)
echo_archs FF_ACT_ARCHS_ALL
;;
*)
echo_usage
exit 1
;;
esac
do-compile-x264.sh 如下:
#! /usr/bin/env bash
#
# Copyright (C) 2014 Miguel Bot髇 <waninkoko@gmail.com>
# Copyright (C) 2014 Zhang Rui <bbcallen@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#--------------------
set -e
if [ -z "$ANDROID_NDK" ]; then
echo "You must define ANDROID_NDK before starting."
echo "They must point to your NDK directories.\n"
exit 1
fi
#--------------------
# common defines
FF_ARCH=$1
if [ -z "$FF_ARCH" ]; then
echo "You must specific an architecture 'arm, armv7a, x86, ...'.\n"
exit 1
fi
FF_BUILD_ROOT=`pwd`
FF_ANDROID_PLATFORM=android-9
ANDROID_NDK=<注意修改為你的ndk目錄>
FF_BUILD_NAME=
FF_SOURCE=
FF_CROSS_PREFIX=
FF_CFG_FLAGS=
FF_PLATFORM_CFG_FLAGS=
FF_EXTRA_CFLAGS=
FF_EXTRA_LDFLAGS=
#--------------------
echo ""
echo "--------------------"
echo "[*] make NDK standalone toolchain"
echo "--------------------"
./tools/do-detect-env.sh
FF_MAKE_TOOLCHAIN_FLAGS=$IJK_MAKE_TOOLCHAIN_FLAGS
FF_MAKE_FLAGS=$IJK_MAKE_FLAG
FF_GCC_VER=$IJK_GCC_VER
FF_GCC_64_VER=$IJK_GCC_64_VER
#----- armv7a begin -----
if [ "$FF_ARCH" = "armv7a" ]; then
echo "gdebug x264-armv7a.............."
FF_BUILD_NAME=x264-armv7a
FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
FF_CROSS_PREFIX=arm-linux-androideabi
FF_TOOLCHAIN_NAME=${FF_CROSS_PREFIX}-${FF_GCC_VER}
FF_PLATFORM_CFG_FLAGS="android-armv7"
elif [ "$FF_ARCH" = "armv5" ]; then
FF_BUILD_NAME=x264-armv5
FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
FF_CROSS_PREFIX=arm-linux-androideabi
FF_TOOLCHAIN_NAME=${FF_CROSS_PREFIX}-${FF_GCC_VER}
FF_PLATFORM_CFG_FLAGS="android"
elif [ "$FF_ARCH" = "x86" ]; then
FF_BUILD_NAME=x264-x86
FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
FF_CROSS_PREFIX=i686-linux-android
FF_TOOLCHAIN_NAME=x86-${FF_GCC_VER}
FF_PLATFORM_CFG_FLAGS="android-x86"
elif [ "$FF_ARCH" = "x86_64" ]; then
FF_ANDROID_PLATFORM=android-21
FF_BUILD_NAME=x264-x86_64
FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
FF_CROSS_PREFIX=x86_64-linux-android
FF_TOOLCHAIN_NAME=${FF_CROSS_PREFIX}-${FF_GCC_64_VER}
FF_PLATFORM_CFG_FLAGS="linux-x86_64"
elif [ "$FF_ARCH" = "arm64" ]; then
FF_ANDROID_PLATFORM=android-21
FF_BUILD_NAME=x264-arm64
FF_SOURCE=$FF_BUILD_ROOT/$FF_BUILD_NAME
FF_CROSS_PREFIX=aarch64-linux-android
FF_TOOLCHAIN_NAME=${FF_CROSS_PREFIX}-${FF_GCC_64_VER}
FF_PLATFORM_CFG_FLAGS="linux-aarch64"
else
echo "unknown architecture $FF_ARCH";
exit 1
fi
FF_TOOLCHAIN_PATH=$FF_BUILD_ROOT/build/$FF_BUILD_NAME/toolchain
FF_SYSROOT=$FF_TOOLCHAIN_PATH/sysroot
FF_PREFIX=$FF_BUILD_ROOT/build/$FF_BUILD_NAME/output
mkdir -p $FF_PREFIX
#mkdir -p $FF_SYSROOT
#--------------------
echo ""
echo "--------------------"
echo "[*] make NDK standalone toolchain"
echo "--------------------"
./tools/do-detect-env.sh
FF_MAKE_TOOLCHAIN_FLAGS=$IJK_MAKE_TOOLCHAIN_FLAGS
FF_MAKE_FLAGS=$IJK_MAKE_FLAG
FF_MAKE_TOOLCHAIN_FLAGS="$FF_MAKE_TOOLCHAIN_FLAGS --install-dir=$FF_TOOLCHAIN_PATH"
FF_TOOLCHAIN_TOUCH="$FF_TOOLCHAIN_PATH/touch"
if [ ! -f "$FF_TOOLCHAIN_TOUCH" ]; then
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh \
$FF_MAKE_TOOLCHAIN_FLAGS \
--platform=$FF_ANDROID_PLATFORM \
--toolchain=$FF_TOOLCHAIN_NAME
touch $FF_TOOLCHAIN_TOUCH;
fi
#--------------------
echo ""
echo "--------------------"
echo "[*] check x264 env"
echo "--------------------"
export PATH=$FF_TOOLCHAIN_PATH/bin:$PATH
export COMMON_FF_CFG_FLAGS=
FF_CFG_FLAGS="$FF_CFG_FLAGS $COMMON_FF_CFG_FLAGS"
#--------------------
# Standard options:
echo "FF_PREFIX = $FF_PREFIX"
FF_CFG_FLAGS="$FF_CFG_FLAGS --prefix=$FF_PREFIX"
FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-static --disable-shared"
FF_CFG_FLAGS="$FF_CFG_FLAGS --enable-pic --enable-strip --disable-asm --disable-cli"
#FF_CFG_FLAGS="$FF_CFG_FLAGS --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi-"
FF_CFG_FLAGS="$FF_CFG_FLAGS --cross-prefix=${FF_CROSS_PREFIX}-"
FF_CFG_FLAGS="$FF_CFG_FLAGS --host=arm-linux"
FF_CFG_FLAGS="$FF_CFG_FLAGS --sysroot=$FF_SYSROOT"
#FF_CFG_FLAGS="$FF_CFG_FLAGS --cross-prefix=${FF_CROSS_PREFIX}-"
#FF_CFG_FLAGS="$FF_CFG_FLAGS $FF_PLATFORM_CFG_FLAGS"
#--------------------
echo ""
echo "--------------------"
echo "[*] configurate x264"
echo "--------------------"
cd $FF_SOURCE && chmod +w configure
#if [ -f "./Makefile" ]; then
# echo 'reuse configure'
#else
echo "./configure $FF_CFG_FLAGS"
./configure $FF_CFG_FLAGS
# --extra-cflags="$FF_CFLAGS $FF_EXTRA_CFLAGS" \
# --extra-ldflags="$FF_EXTRA_LDFLAGS"
#fi
#--------------------
echo ""
echo "--------------------"
echo "[*] compile x264"
echo "--------------------"
make depend
echo "make $FF_MAKE_FLAGS"
make $FF_MAKE_FLAGS
make install
#--------------------
echo ""
echo "--------------------"
echo "[*] link x264"
echo "--------------------"
然后運(yùn)行compile-x264.sh腳本共啃,一切順利的話可以在android/contrib/build目錄下輸出x264相關(guān)庫,如下圖:(這里只編譯了armv7a類型)
2.2 編譯openssl
因?yàn)轫?xiàng)目需要概漱,需要支持https協(xié)議格式的視頻丑慎。如果項(xiàng)目無需支持https協(xié)議可以略過這一步。
進(jìn)入<ijkplayer主目錄>/android/contrib
,執(zhí)行以下命令:
./compile-openssl.sh clean
./compile-openssl.sh all
ps. all 可以替換為需要的cpu類型瓤摧。
如果編譯過程出現(xiàn)invalid preprocessing directive #undefine
錯(cuò)誤竿裂,按照錯(cuò)誤提示打開termbits.h文件,將#undefine 的這一行注釋掉即可照弥。
如果一切順利腻异,編譯成功后在<ijkplayer主目錄>/android/contrib
目錄下生成以下openssl文件
2.3 編譯FFmpeg
進(jìn)入ijkplayer主目錄,使用default腳本这揣,首先進(jìn)行軟鏈:
cd config
rm module.sh
ln -s module-default.sh module.sh
然后修改module-default.sh
腳本悔常,左邊為ijkplayer默認(rèn)配置,右邊為修改配置
添加以下編譯選項(xiàng)
# Add by 21cn
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-small"
# set protocols
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=concat"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=crypto"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=data"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ffrtmpcrypt"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ffrtmphttp"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=gopher"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=icecast"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ijkhttphook"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ijkio"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ijklongurl"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ijkmediadatasource"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ijksegment"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=ijktcphook"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=md5"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=mmsh"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=mmst"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=pipe"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=prompeg"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=tee"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=unix"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-protocol=subfile"
#set muxers
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-muxers"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-muxer=h264"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-muxer=mp3"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-muxer=mp4"
#set encoders
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-encoders"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-libx264"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-encoder=libx264"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-encoder=aac"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-encoder=mp3"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-encoder=mpeg4"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-debug"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-linux-perf"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --extra-cflags=-I/Users/littlebyte/develop/ijkplayer-android/android/contrib/build/x264-armv7a/output/include"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --extra-ldflags=-L/Users/littlebyte/develop/ijkplayer-android/android/contrib/build/x264-armv7a/output/lib"
# End of add
以上添加最主要是增加了libx264編碼器给赞,并且進(jìn)行了一定的精簡(jiǎn)机打。
最后進(jìn)入 android/contrib,運(yùn)行以下命令:
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh armv7a
因?yàn)槲抑恍枰猘rmv7a 的cpu類型的so庫片迅,就只編譯armv7a残邀,如果需要其他類型cpu的so庫,可以armv7a修改為x86等cpu障涯,如果需要編譯全部cpu類型改為如下命令:
./compile-ffmpeg.sh all
以上命令執(zhí)行完后罐旗,如果沒有問題等話在android/contrib目錄下生成ffmpeg 對(duì)應(yīng)cpu類型的目錄
2.2 編譯IjkPlayer
cd ..
./compile-ijk.sh all
執(zhí)行以上命令后,在主目錄的ijkplayer目錄下生成最終需要的so庫 唯蝶。