[Android Development] How to build Ffmpeg extension for integrating into Exoplayer

Thế Dũng
4 min readAug 5, 2021

--

Hi all, hope you guys doing well!

TL;DR: Read from first Big bold text below.

I’ve just met a problem with Exoplayer in using to playing UDP stream. Video is coming but the audio is not. Then I start to searching for the reason. Very firstly you can find this https://github.com/google/ExoPlayer/issues/6843. But I asked video-encoder team and they answered me that they are using AAC, not Ffmpeg. So, I confused for a day and decided to using VLC to find what audio codec is using(Tools > Codec information). And I found that it was MPEG Audio. So, I must add Ffmpeg extension into my player.

And a very impressive thing is waiting for me. The Ffmpeg extension document. I have official document and wrestled with it for 2 days. To generate a small extension for my player. Document here. So I decided to write this article to somehow, save your time.

I using windows and mac os. And I can not build the extension with Windows (Powershell as recommend). So mac os is my hope.

How can I build Ffmpeg Extension

I used MacOS to generate this extension, I think Linux will be much easier but I don’t have linux in my room. (Linux should be the same — without weird error)

Open ExoPlayer FFmpeg extension.

Prepare for start: You need to permit terminal to doing what it want by:

Goto system preferences > Security & Privacy > Developer Tools > Allow terminal. And remember to quit all terminal. We need to do that to apply the change.

If you do not doing the above setting, you can meet some error like

“clang” cannot be opened because the developer cannot be verified.

And it is annoying.

Ok, now we can start!

First of all, git clone exoplayer. Very easy.

Second, open terminal and cd to Exoplayer project location.

EXOPLAYER_ROOT="$(pwd)"
FFMPEG_EXT_PATH="${EXOPLAYER_ROOT}/extensions/ffmpeg/src/main"
NDK_PATH="<path to Android NDK>"

NOTE: If you have in your mind that using Android NDK been used in Android studio to fill in this field… Do not do that

I don’t know why but it make the command build get error (like could not found config.mak, don’t have xyz directories… I don’t remember)

So which NDK should we use? I use NDK r22b. The newest stable NDK. I’ve tried NDK r15 and r21e and they didn’t work for me. You can download NDK from here. I think you should find for STABLE version to make things work.

When you have NDK on your disk, decompress and pass the Path into your terminal. It should look like this:

Then, define the HOST PLATFORM (For Linux is linux-x86_64 )

HOST_PLATFORM="darwin-x86_64"

Then

cd "<preferred location for ffmpeg>" && \
git clone git://source.ffmpeg.org/ffmpeg && \
cd ffmpeg && \
git checkout release/4.2 && \
FFMPEG_PATH="$(pwd)"

Place <preferred location for ffmpeg> with your location (you can create a new folder and place it with the name of folder)

ENABLED_DECODERS=(mp3)

In this line, I’ve just wanted to build for support mp3 (MPEG/L1, L2) and hit enter. In case you want to build for other format, see this support page.

cd "${FFMPEG_EXT_PATH}/jni" && \
./build_ffmpeg.sh \
"${FFMPEG_EXT_PATH}" "${NDK_PATH}" "${HOST_PLATFORM}" "${ENABLED_DECODERS[@]}"

After enter the above commands, you should see the terminal running with a lot of text print in screen.

You’ve done the build native library for render MPEG Audio. One more step is to build aar file.

Open the exoplayer project that you have clone from the first step. If an error say that invalid revision: 3.18.1…. you can fix it by downgrade the CMake use in project. See this.

Then when the project is loading success, typing command in Android Studio terminal (Of course you can write it in separate terminal with the same cd location)

./gradlew extension-ffmpeg:assembleRelease

And the terminal will run.

At this step, if you meet error related to Java:

task :library-common:compileReleaseJavaWithJavac FAILED

Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains a valid JDK installation

This mean that your build is error. And to fix, see this. You can try the green-check answer, but in my case, the second answer work. So I reference it in this post.

And run the ./gradlew above again.

After the build success, you go and get the aar in ffmpeg buildout folder and import into your project.

/ExoPlayer/extensions/ffmpeg/buildout/outputs/aar

BUILD DONE

Next, you want to use it in your code?

Import the aar library into your project using gradle. This should be easy, so I don’t put it here. (I just search and this is result)

Log.d("Check", "${FfmpegLibrary.isAvailable()}")mRenderFactory.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)

Check the available of extension with FfmpegLibrary.isAvailable()

And setup for render factory to use extension if needed.

It’s done here. The application can render MPEG Audio now.

Thanks for reading!

Have a nice coding time!

--

--

Thế Dũng
Thế Dũng

Written by Thế Dũng

Software engineer - Mobile developer

No responses yet