Some time ago, I received a demand for playing video stream, so I searched many open source frameworks on the Internet, and finally found that iJkPlayer produced by Bilibili can perfectly meet my demand. Since I am an Android developer, I am used to compiling so library using Cywin. There are many problems in compiling ijkPlayer with Cywin; Thus came this article.

1. Preparation before compilation

Configure the Android NDK environment variables

export ANDROID_NDK=/cygdrive/e/ndk-win/android-ndk-r10e
install git
install yasm
install g++
install make
Copy the code

Note: NDK is r10E as suggested by readme on bilili; If you are using Cywin, the NDK requires the Windows NDK

Modify the Android.mk file (required for Cywin compilation)

Ijkplayer \Android \ijkplayer\

SRC \main\jni\ android. mk file. If you do not modify this file, compile using compile- ijkk. sh all will cause errors.

The reason is:

Ijkplayer android ijkplayer player-armv7a SRC main jni Ijkmedia and Android-Ndk-Prof link to/ijkMedia and /ijkprof respectively in the root directory of ijkPlayer, while Cygwin and Android NDK seem to have problems handling folder soft links. So compilation of iJKMedia and iJKProf is not performed.

Modification method:

1. Write ijkMedia and ijkProf makefiles at the end of ijkPlayer \Android \ ijkPlayer \player-armv7a\ SRC \main\jni\ android. mk and include them.

include /home/AriaL/ijkplayer/ijkplayer-android/ijkmedia/*.mk
include /home/AriaL/ijkplayer/ijkplayer-android/ijkprof/android-ndk-profiler-dummy/jni/*.mk
Copy the code

2. Delete ijkMedia and Android-ndk-prof under \ ijkPlayer \ Android \ ijkPlayer \player-armv7a\ SRC \main\jni

3. Comment out the part of the android-ndk-prof link file generated in /android/compile-ijk.sh

do_sub_cmd () { SUB_CMD=$1 # if [ -L "./android-ndk-prof" ]; then # rm android-ndk-prof # fi # if [ "$PARAM_SUB_CMD" = 'prof' ]; then # echo 'profiler build: YES'; # ln -s .. /.. /.. /.. /.. /.. /ijkprof/android-ndk-profiler/jni android-ndk-prof # else # echo 'profiler build: NO'; # ln -s .. /.. /.. /.. /.. /.. /ijkprof/android-ndk-profiler-dummy/jni android-ndk-prof # fi case $SUB_CMD in prof) $ANDROID_NDK/ndk-build $FF_MAKEFLAGS ;; clean) $ANDROID_NDK/ndk-build clean ;; rebuild) $ANDROID_NDK/ndk-build clean $ANDROID_NDK/ndk-build $FF_MAKEFLAGS ;; *) $ANDROID_NDK/ndk-build $FF_MAKEFLAGS ;; esac }Copy the code

TIP: The other ABI modifies the MK file in the corresponding SRC folder

X86 and other ABI modifications

As with armv7a, you need to copy jin/ anroid. mk from armv7a to jni/ on x86

Second, modify,module-lite.shfile

Modify the module-lite.sh file to open ijkPlayer which is off by default while we need features such as live streaming, RMVB support and so on.

Enable the live Stream (RSTP \ RTP) configuration

In ijkplayer/config, go to the module-lite.sh file, which is the configuration file for compiling FFmpeg, and configure which modules to turn on and which modules to turn off. To enable the RTSP protocol, go to export COMMON_FF_CFG_FLAGS=”$COMMON_FF_CFG_FLAGS –disable-protocol= RTP “. Export COMMON_FF_CFG_FLAGS=”$COMMON_FF_CFG_FLAGS –enable-protocol= RTP “then add the following line, Open the RTSP audio and video separator export COMMON_FF_CFG_FLAGS=”$COMMON_FF_CFG_FLAGS –enable-demuxer= RTSP “and run the following command to connect the configuration file

To avoid the error of missing a CODEC with ID 8, add the following code

export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-decoder=mjpeg"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-demuxer=mjpeg"
Copy the code

MKV, RMVB support

Modify the module-lite.sh file to add the following code

export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-muxer=matroska export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-muxer=matroska_audio export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS  --enable-demuxer=matroskaCopy the code

More format support

Third, perform compilation

Git clone https://github.com/Bilibili/ijkplayer.git ijkplayer - android CD ijkplayer - android git checkout - B latest k0.8.8  ./init-android.sh cd android/contrib ./compile-ffmpeg.sh clean ./compile-ffmpeg.sh all cd .. ./compile-ijk.sh allCopy the code

Cywin soft connection execution precautions

Arm-linux-androideabi-gcc: Argument list too long error

If you use ln -s module-default.sh modul. sh in Cywin, the preceding error occurs. You can circumvent this problem by linking ln -s module-lite.sh modul. sh and then modifying the configuration in module-lite.sh as needed

Four, other

Ijkplayer output directory after compilation

ijkplayer\ijkplayer-android\android\ijkplayer\ijkplayer-armv7a\src\main\libs

IP address of the public network live stream test

Reference links: blog.csdn.net/renhui1112/…

  1. The RTMP protocol live source Hong Kong TV: rtmp://live.hkstv.hk.lxdns.com/live/hks

  2. RTSP protocol live source bear rabbit (VOD) : RTSP: / / 184.72.239.149 / VOD/mp4: / / BigBuckBunny_175k. Mov foreign television: rtsp://rtsp-v3-spbtv.msk.spbtv.com/spbtv_v3_1/214_110.sdp

  3. The HTTP protocol live source http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8

Other ways to obtain live address:

  • Use Charles to intercept live addresses from other apps
  • Use tools for local distribution

Android IjkPlayer configuration

After compiling the SO library. It can be used in Android and some configuration is required to support live streaming.

mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1); // mmediaPlayer. setOption(ijkMediaPlayer. OPT_CATEGORY_PLAYER, "VideoToolbox ", 0); / / RTSP set https://ffmpeg.org/ffmpeg-protocols.html#rtsp mMediaPlayer. SetOption (IjkMediaPlayer OPT_CATEGORY_FORMAT, "rtsp_transport", "tcp"); mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "rtsp_flags", "prefer_tcp"); mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "allowed_media_types", "video"); SetOption (ijkMediaPlayer. OPT_CATEGORY_FORMAT, "timeout", 20000); mmediaPlayer. setOption(ijkMediaPlayer. OPT_CATEGORY_FORMAT, "timeout", 20000); mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "buffer_size", 1316); mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "infbuf", 1); Mmediaplayer. setOption(ijkMediaplayer. OPT_CATEGORY_FORMAT, "analyzemaxduration", 100L); mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "probesize", 10240L); mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "flush_packets", 1L); // Turn off the player buffer, this must be turned off, otherwise it will appear that after playing for a period of time, the main has been stuck, Console prints FFP_MSG_BUFFERING_START mMediaPlayer.setOption(ijkMediaplayer. OPT_CATEGORY_PLAYER, "packet-buffering", 0L); mMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1L);Copy the code

Note: When the network changes or is disconnected, the live broadcast will stop automatically and the console will print a message that ijkMP_GEt_MSG: FFP_MSG_COMPLETED has been completed. The solution is to recreate the player in OnCompletionListener

mMediaPlayer.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() { @Override public void OnCompletion (IMediaPlayer IMediaPlayer) {// Restart (); T.shoshort (getContext(), "Network disconnected, reconnecting..." ); }});Copy the code

Recreate the MediaPlayer

Timer mTimer; private void restart() { if (mTimer ! = null) { mTimer.purge(); mTimer.cancel(); } mMediaPlayer.release(); showLoadingDialog(); mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { if (NetUtils.isConnected(getContext())) { initMediaPlay(mHolder, mPlayOpt.url); mTimer.purge(); mTimer.cancel(); }}}, 1000, 1000); }Copy the code

Some other questions

RSTP green screen cannot be hard unplayed in 4K HD video

Five, the final effect

flow_live

The effect is still good source code