Skip to content

FFmpeg Runtime

WangBin edited this page Sep 7, 2019 · 25 revisions

FFmpeg is heavily used in some mdk modules, for example demuxer, muxer and software decoder. mdk main library does not depends on ffmpeg, and all modules can be implemented by platform dependent APIs. Previously, ffmpeg(LGPL) static libraries are linked into mdk-avglue plugin. Since v0.4.0, ffmpeg is loaded dynamically by default(except iOS), and avglue module is merged into main library(default). There are some benefits:

  1. Reduce the whole size. If FFmpeg exists in system, for example linux distributions, or already used somewhere else in your app, then the existing ffmpeg runtime can be reused. The size of app can be decreased considerably, from 5M to 40M.
  2. SDK can be used on more platforms. Linux library versioning and symbol versioning are not friendly to developers to make softwares compatible on different distributions. FFmpeg hardware accelerated codec VA-API depends on libva, whose major version changed before. On ubuntu 16.04 LTS, it's libva.so.1, but on 18.04 LTS, it becomes libva.so.2. So to support both OSes, there must be 2 mdk-avglue binaries. If ffmpeg is loaded at runtime, user can decide to build ffmpeg against each OS.

Select FFmpeg Runtime

Since v0.4.0, a single ffmpeg share library is included in sdk, and will be loaded at runtime. You can use this if no special demand. MDK is compatible with ffmpeg 4.0 or later. To use system ffmpeg libraries, or the ones in your app, you can

  • delete ffmpeg runtime in sdk
  • If existing ffmpeg libraries are in library loading search dirs, and their names are standard, then they will be loaded automatically.
  • Otherwise, you can
    • set environment vars AVUTIL_LIB, AVCODEC_LIB, AVFORMAT_LIB, AVFILTER_LIB, SWRESAMPLE_LIB and SWSCALE_LIB with ffmpeg module path.
    • call SetGlobalOption(key, value) once(usually before other mdk calls), where key is avutil_lib, avcodec_lib, avformat_lib, avfilter_lib, swresample_lib and swscale_lib, value is corresponding path. For example `SetGlobalOption("avutil_lib", "/opt/lib/libavutil.so.56")

Once ffmpeg is loaded, SetGlobalOption(key, value) can be called again if you need.

Runtime Lookup

If ffmpeg any module is not set manually by environment vars or SetGlobalOption(key, value), it's searched in the following order

dir: current mdk module dir > mdk framework dir(apple) > system default search dir file name: single ffmpeg library > ffmpeg modules w/ version > ffmpeg modules w/o version(last)

For example, on windows, mdk will search avutil like this: ffmpeg.dll in mdk.dll dir=>avutil-56.dll in mdk.dll dir=>ffmpeg.dll in %PATH%=>avutil-56.dll in %PATH%=>avutil.dll in mdk.dll dir=> avutil.dll in %PATH%

macOS

If sandbox is enabled in your app, libffmpeg.dylib or ffmpeg dylibs MUST be moved to mdk.framework/Versions/Current/

Build FFmpeg

https://github.com/wang-bin/avbuild

Clone this wiki locally