Skip to content

FFmpeg Runtime

WangBin edited this page Aug 22, 2020 · 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 choose one of
    • 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")
    • call SetGlobalOption(key, value) once(usually before other mdk calls), where key is avutil, avcodec, avformat, avfilter, swresample and swscale, value is corresponding module hande(from dlopen(), GetModuleHandle() or LoadLibrary()) of type void*. For example SetGlobalOption("avutil", (void*)avutil_handle). This is the only way to select ffmpeg runtime modules on macOS 10.15+.

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 w/ version > single ffmpeg library w/o version > ffmpeg modules w/ version > ffmpeg modules w/o version(last)

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

Minimal Module Sets

The following modules are always required

  • avutil
  • avcodec
  • avformat

Depending on the features(resample, filter) you need, other ffmpeg modules can be added.

macOS

Since macOS 10.15, dlopen is not allowed in app by default, so mdk has to (weak)link against ffmpeg(libffmpeg.4.dylib) and standard ffmpeg 4.x libraries. If you want to use your own ffmpeg libraries, you have to choices:

    auto avutil = dlopen("libavutil.56.dylib", RTLD_NOLOAD|RTLD_LOCAL|RTLD_NOW); // RTLD_NOLOAD: libavutil.56.dylib is already loaded by linker if ffmpeg libraries exists in mdk's rpath(@loader_path, @loader_path/lib, @executable_path/../Frameworks, @loader_path/../lib, /usr/local/lib)
    SetGlobalOptions("avutil", avutil);
    // set other ffmpeg modules
  • Build your own libffmpeg and replace the one in mdk sdk, then make symbolic links to standard ffmpeg modules such as libavutil, libavcodec if your app depends on these.

Build FFmpeg

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

Clone this wiki locally