A film file is often a bundle of tracks: one video track and several audio tracks, each in its own format. Devices differ in which audio decoders they include, so the same file can be easy for one television and a stretch for the next. Yonto Player is built on media3 (ExoPlayer) on Android, and this post is about how it carries software audio decoders behind the device's own, so more formats play on more devices.
The rule: the device first, software second
The design is two sentences. The device's own decoders come first in the renderer list, so they are preferred when they can take a track. The software decoders come after them, for the tracks the device's cannot take.
That order matters for two reasons. A decoder the device ships is built for that device, and a software decoder spends CPU time on a small box that may have little to spare. So the software one is the fallback, not the default.
media3 gives you a natural place to express the rule: the renderers factory. ExoPlayer holds a list of renderers, and for each track in a file it asks them what they can handle. The platform's MediaCodec audio renderer is added first, and a renderer you append goes after it, so the device's comes first in that list and the appended one is there for the tracks the device's cannot take. This is a simplified sketch of the pattern, with generic names and without the app's other settings:
// Simplified sketch, not the exact source.
class AppRenderersFactory(context: Context) : DefaultRenderersFactory(context) {
override fun buildAudioRenderers(
context: Context,
extensionRendererMode: Int,
mediaCodecSelector: MediaCodecSelector,
enableDecoderFallback: Boolean,
audioSink: AudioSink,
eventHandler: Handler,
eventListener: AudioRendererEventListener,
out: ArrayList<Renderer>,
) {
// The device's own decoders are added first.
super.buildAudioRenderers(
context, extensionRendererMode, mediaCodecSelector,
enableDecoderFallback, audioSink, eventHandler, eventListener, out,
)
// The software decoders go after them.
out.add(FfmpegAudioRenderer(eventHandler, eventListener, audioSink))
}
}
The software renderer answers the "can you handle this track" question from the library itself. If the native library is not available or does not know the format, it reports the track as unsupported when the library has no decoder for the format.
Which formats
The software decoders cover AC3 (Dolby Digital), E-AC-3 (Dolby Digital Plus), DTS including DTS-HD tracks, Dolby TrueHD, FLAC, Vorbis, Opus, ALAC and the WMA family. In the build, that is a list of fourteen FFmpeg decoder names: ac3, eac3, dca, truehd, flac, vorbis, opus, alac, wmav1, wmav2, wmapro, wmalossless, pcm_mulaw and pcm_alaw. A small mapping from media3's MIME types to those decoder names decides which tracks the renderer claims.
The public page about this, DTS, TrueHD and Dolby audio on Android TV, explains it in plain words for a viewer.
Where the library comes from
media3 ships an FFmpeg extension, but not as a ready-made artifact on Maven. The extension is Java plus a small piece of JNI, and you are meant to build FFmpeg yourself and link it. So the repo keeps media3's extension sources in a module of their own, core/ffmpeg, and builds FFmpeg from its release tarball as part of the Gradle build.
The build is a custom Gradle task. Here is its shape in outline, again simplified:
// Simplified outline of the build task.
abstract class BuildFfmpeg : DefaultTask() {
@get:Input abstract val version: Property<String> // a release number
@get:Input abstract val sha256: Property<String> // the tarball's expected hash
@get:Input abstract val decoders: ListProperty<String> // the decoder list above
@get:Input abstract val abis: ListProperty<String>
@TaskAction
fun build() {
val source = downloadAndVerify() // fetch once, check the hash, unpack
for (abi in abis.get()) {
// configure with the NDK toolchain for this ABI, then make
run("$source/configure", abiFlags(abi) + commonFlags(decoders.get()))
run("make", "install-libs", "install-headers")
}
}
}
The tarball is pinned by version and by SHA-256. The download is checked before it is unpacked, and a copy is kept in the Gradle user home so a clean does not fetch it again.
The build is a whitelist. The flags start from --disable-everything and then switch on exactly the listed decoders. There is no FFmpeg command-line program, no container parsing (media3 does that), no network code, no filters and no scaler, only the audio codec library and the resampler. Most of FFmpeg is simply not in the binary.
The libraries are LGPL shared libraries, built as separate .so files rather than linked statically. The flags include neither --enable-gpl nor --enable-nonfree, and a unit test in build-logic reads the flag list and fails if either, or a flag that pulls in an external library, appears.
CMake then links the small JNI library to the shared libraries the task wrote, and Gradle makes CMake wait for the task.
The cost
Nothing here is free, so here is what it costs.
-
Native libraries per ABI. The build targets
arm64-v8aandarmeabi-v7a, the two ARM ABIs. Each ABI carries its own copy of the three FFmpeg libraries plus the JNI one. -
A longer first build. An FFmpeg build takes about half a minute on a development machine, and the first build on a machine needs the network, the NDK and
make. - Software decoding uses the CPU. That is one reason the order is device first. A DTS or TrueHD stream decoded in software is more work than one the device's own chip handles, which is acceptable as the fallback and not as the default.
- An upgrade has a procedure. The extension's Java and JNI are copied from a media3 version, so bumping media3 means copying the new files over them and reading what changed in the JNI file. R8 also has to keep the two classes the native code looks up by name.
What to take from it
If you build on media3 and your audience includes older or cheaper boxes, two ideas carry over. Put the software decoder after the device's, in the renderers factory, so the order encodes the preference. And build the codec library as a whitelist with a pinned, hash-checked source, and make the licence flags something a test holds, not something a person remembers.
Top comments (0)