1*30877f79SAndroid Build Coastguard Worker# ExoPlayer FFmpeg module 2*30877f79SAndroid Build Coastguard Worker 3*30877f79SAndroid Build Coastguard WorkerThe FFmpeg module provides `FfmpegAudioRenderer`, which uses FFmpeg for decoding 4*30877f79SAndroid Build Coastguard Workerand can render audio encoded in a variety of formats. 5*30877f79SAndroid Build Coastguard Worker 6*30877f79SAndroid Build Coastguard Worker## License note 7*30877f79SAndroid Build Coastguard Worker 8*30877f79SAndroid Build Coastguard WorkerPlease note that whilst the code in this repository is licensed under 9*30877f79SAndroid Build Coastguard Worker[Apache 2.0][], using this module also requires building and including one or 10*30877f79SAndroid Build Coastguard Workermore external libraries as described below. These are licensed separately. 11*30877f79SAndroid Build Coastguard Worker 12*30877f79SAndroid Build Coastguard Worker[Apache 2.0]: https://github.com/google/ExoPlayer/blob/release-v2/LICENSE 13*30877f79SAndroid Build Coastguard Worker 14*30877f79SAndroid Build Coastguard Worker## Build instructions (Linux, macOS) 15*30877f79SAndroid Build Coastguard Worker 16*30877f79SAndroid Build Coastguard WorkerTo use the module you need to clone this GitHub project and depend on its 17*30877f79SAndroid Build Coastguard Workermodules locally. Instructions for doing this can be found in the 18*30877f79SAndroid Build Coastguard Worker[top level README][]. The module is not provided via Google's Maven repository 19*30877f79SAndroid Build Coastguard Worker(see [#2781][] for more information). 20*30877f79SAndroid Build Coastguard Worker 21*30877f79SAndroid Build Coastguard WorkerIn addition, it's necessary to manually build the FFmpeg library, so that gradle 22*30877f79SAndroid Build Coastguard Workercan bundle the FFmpeg binaries in the APK: 23*30877f79SAndroid Build Coastguard Worker 24*30877f79SAndroid Build Coastguard Worker* Set the following shell variable: 25*30877f79SAndroid Build Coastguard Worker 26*30877f79SAndroid Build Coastguard Worker``` 27*30877f79SAndroid Build Coastguard Workercd "<path to project checkout>" 28*30877f79SAndroid Build Coastguard WorkerFFMPEG_MODULE_PATH="$(pwd)/extensions/ffmpeg/src/main" 29*30877f79SAndroid Build Coastguard Worker``` 30*30877f79SAndroid Build Coastguard Worker 31*30877f79SAndroid Build Coastguard Worker* Download the [Android NDK][] and set its location in a shell variable. 32*30877f79SAndroid Build Coastguard Worker This build configuration has been tested on NDK r21. 33*30877f79SAndroid Build Coastguard Worker 34*30877f79SAndroid Build Coastguard Worker``` 35*30877f79SAndroid Build Coastguard WorkerNDK_PATH="<path to Android NDK>" 36*30877f79SAndroid Build Coastguard Worker``` 37*30877f79SAndroid Build Coastguard Worker 38*30877f79SAndroid Build Coastguard Worker* Set the host platform (use "darwin-x86_64" for Mac OS X): 39*30877f79SAndroid Build Coastguard Worker 40*30877f79SAndroid Build Coastguard Worker``` 41*30877f79SAndroid Build Coastguard WorkerHOST_PLATFORM="linux-x86_64" 42*30877f79SAndroid Build Coastguard Worker``` 43*30877f79SAndroid Build Coastguard Worker 44*30877f79SAndroid Build Coastguard Worker* Fetch FFmpeg and checkout an appropriate branch. We cannot guarantee 45*30877f79SAndroid Build Coastguard Worker compatibility with all versions of FFmpeg. We currently recommend version 4.2: 46*30877f79SAndroid Build Coastguard Worker 47*30877f79SAndroid Build Coastguard Worker``` 48*30877f79SAndroid Build Coastguard Workercd "<preferred location for ffmpeg>" && \ 49*30877f79SAndroid Build Coastguard Workergit clone git://source.ffmpeg.org/ffmpeg && \ 50*30877f79SAndroid Build Coastguard Workercd ffmpeg && \ 51*30877f79SAndroid Build Coastguard Workergit checkout release/4.2 && \ 52*30877f79SAndroid Build Coastguard WorkerFFMPEG_PATH="$(pwd)" 53*30877f79SAndroid Build Coastguard Worker``` 54*30877f79SAndroid Build Coastguard Worker 55*30877f79SAndroid Build Coastguard Worker* Configure the decoders to include. See the [Supported formats][] page for 56*30877f79SAndroid Build Coastguard Worker details of the available decoders, and which formats they support. 57*30877f79SAndroid Build Coastguard Worker 58*30877f79SAndroid Build Coastguard Worker``` 59*30877f79SAndroid Build Coastguard WorkerENABLED_DECODERS=(vorbis opus flac) 60*30877f79SAndroid Build Coastguard Worker``` 61*30877f79SAndroid Build Coastguard Worker 62*30877f79SAndroid Build Coastguard Worker* Add a link to the FFmpeg source code in the FFmpeg module `jni` directory. 63*30877f79SAndroid Build Coastguard Worker 64*30877f79SAndroid Build Coastguard Worker``` 65*30877f79SAndroid Build Coastguard Workercd "${FFMPEG_MODULE_PATH}/jni" && \ 66*30877f79SAndroid Build Coastguard Workerln -s "$FFMPEG_PATH" ffmpeg 67*30877f79SAndroid Build Coastguard Worker``` 68*30877f79SAndroid Build Coastguard Worker 69*30877f79SAndroid Build Coastguard Worker* Execute `build_ffmpeg.sh` to build FFmpeg for `armeabi-v7a`, `arm64-v8a`, 70*30877f79SAndroid Build Coastguard Worker `x86` and `x86_64`. The script can be edited if you need to build for 71*30877f79SAndroid Build Coastguard Worker different architectures: 72*30877f79SAndroid Build Coastguard Worker 73*30877f79SAndroid Build Coastguard Worker``` 74*30877f79SAndroid Build Coastguard Workercd "${FFMPEG_MODULE_PATH}/jni" && \ 75*30877f79SAndroid Build Coastguard Worker./build_ffmpeg.sh \ 76*30877f79SAndroid Build Coastguard Worker "${FFMPEG_MODULE_PATH}" "${NDK_PATH}" "${HOST_PLATFORM}" "${ENABLED_DECODERS[@]}" 77*30877f79SAndroid Build Coastguard Worker``` 78*30877f79SAndroid Build Coastguard Worker 79*30877f79SAndroid Build Coastguard Worker## Build instructions (Windows) 80*30877f79SAndroid Build Coastguard Worker 81*30877f79SAndroid Build Coastguard WorkerWe do not provide support for building this module on Windows, however it should 82*30877f79SAndroid Build Coastguard Workerbe possible to follow the Linux instructions in [Windows PowerShell][]. 83*30877f79SAndroid Build Coastguard Worker 84*30877f79SAndroid Build Coastguard Worker[Windows PowerShell]: https://docs.microsoft.com/en-us/powershell/scripting/getting-started/getting-started-with-windows-powershell 85*30877f79SAndroid Build Coastguard Worker 86*30877f79SAndroid Build Coastguard Worker## Using the module 87*30877f79SAndroid Build Coastguard Worker 88*30877f79SAndroid Build Coastguard WorkerOnce you've followed the instructions above to check out, build and depend on 89*30877f79SAndroid Build Coastguard Workerthe module, the next step is to tell ExoPlayer to use `FfmpegAudioRenderer`. How 90*30877f79SAndroid Build Coastguard Workeryou do this depends on which player API you're using: 91*30877f79SAndroid Build Coastguard Worker 92*30877f79SAndroid Build Coastguard Worker* If you're passing a `DefaultRenderersFactory` to `ExoPlayer.Builder`, you 93*30877f79SAndroid Build Coastguard Worker can enable using the module by setting the `extensionRendererMode` parameter 94*30877f79SAndroid Build Coastguard Worker of the `DefaultRenderersFactory` constructor to 95*30877f79SAndroid Build Coastguard Worker `EXTENSION_RENDERER_MODE_ON`. This will use `FfmpegAudioRenderer` for 96*30877f79SAndroid Build Coastguard Worker playback if `MediaCodecAudioRenderer` doesn't support the input format. Pass 97*30877f79SAndroid Build Coastguard Worker `EXTENSION_RENDERER_MODE_PREFER` to give `FfmpegAudioRenderer` priority over 98*30877f79SAndroid Build Coastguard Worker `MediaCodecAudioRenderer`. 99*30877f79SAndroid Build Coastguard Worker* If you've subclassed `DefaultRenderersFactory`, add an `FfmpegAudioRenderer` 100*30877f79SAndroid Build Coastguard Worker to the output list in `buildAudioRenderers`. ExoPlayer will use the first 101*30877f79SAndroid Build Coastguard Worker `Renderer` in the list that supports the input media format. 102*30877f79SAndroid Build Coastguard Worker* If you've implemented your own `RenderersFactory`, return an 103*30877f79SAndroid Build Coastguard Worker `FfmpegAudioRenderer` instance from `createRenderers`. ExoPlayer will use 104*30877f79SAndroid Build Coastguard Worker the first `Renderer` in the returned array that supports the input media 105*30877f79SAndroid Build Coastguard Worker format. 106*30877f79SAndroid Build Coastguard Worker* If you're using `ExoPlayer.Builder`, pass an `FfmpegAudioRenderer` in the 107*30877f79SAndroid Build Coastguard Worker array of `Renderer`s. ExoPlayer will use the first `Renderer` in the list 108*30877f79SAndroid Build Coastguard Worker that supports the input media format. 109*30877f79SAndroid Build Coastguard Worker 110*30877f79SAndroid Build Coastguard WorkerNote: These instructions assume you're using `DefaultTrackSelector`. If you have 111*30877f79SAndroid Build Coastguard Workera custom track selector the choice of `Renderer` is up to your implementation, 112*30877f79SAndroid Build Coastguard Workerso you need to make sure you are passing an `FfmpegAudioRenderer` to the player, 113*30877f79SAndroid Build Coastguard Workerthen implement your own logic to use the renderer for a given track. 114*30877f79SAndroid Build Coastguard Worker 115*30877f79SAndroid Build Coastguard Worker[top level README]: https://github.com/google/ExoPlayer/blob/release-v2/README.md 116*30877f79SAndroid Build Coastguard Worker[Android NDK]: https://developer.android.com/tools/sdk/ndk/index.html 117*30877f79SAndroid Build Coastguard Worker[#2781]: https://github.com/google/ExoPlayer/issues/2781 118*30877f79SAndroid Build Coastguard Worker[Supported formats]: https://exoplayer.dev/supported-formats.html#ffmpeg-extension 119*30877f79SAndroid Build Coastguard Worker 120*30877f79SAndroid Build Coastguard Worker## Using the module in the demo application 121*30877f79SAndroid Build Coastguard Worker 122*30877f79SAndroid Build Coastguard WorkerTo try out playback using the module in the [demo application][], see 123*30877f79SAndroid Build Coastguard Worker[enabling extension decoders][]. 124*30877f79SAndroid Build Coastguard Worker 125*30877f79SAndroid Build Coastguard Worker[demo application]: https://exoplayer.dev/demo-application.html 126*30877f79SAndroid Build Coastguard Worker[enabling extension decoders]: https://exoplayer.dev/demo-application.html#enabling-extension-decoders 127*30877f79SAndroid Build Coastguard Worker 128*30877f79SAndroid Build Coastguard Worker## Links 129*30877f79SAndroid Build Coastguard Worker 130*30877f79SAndroid Build Coastguard Worker* [Troubleshooting using extensions][] 131*30877f79SAndroid Build Coastguard Worker* [Javadoc][] 132*30877f79SAndroid Build Coastguard Worker 133*30877f79SAndroid Build Coastguard Worker[Troubleshooting using extensions]: https://exoplayer.dev/troubleshooting.html#how-can-i-get-a-decoding-extension-to-load-and-be-used-for-playback 134*30877f79SAndroid Build Coastguard Worker[Javadoc]: https://exoplayer.dev/doc/reference/index.html 135