Skip to content

Commit

Permalink
Olga's basic libav framework (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
classilla committed Mar 17, 2019
1 parent 8868813 commit 284830e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
54 changes: 54 additions & 0 deletions dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "mozilla/Preferences.h"
#include "prlink.h"

#ifdef XP_DARWIN
#include <dlfcn.h>
#include <libgen.h>
#include <mach-o/dyld.h>
#endif /* XP_DARWIN */

namespace mozilla
{

Expand Down Expand Up @@ -40,7 +46,11 @@ static const char* sLibs[] = {
#endif
};

#ifdef XP_DARWIN
void* FFmpegRuntimeLinker::sLinkedLib = nullptr;
#else
PRLibrary* FFmpegRuntimeLinker::sLinkedLib = nullptr;
#endif /* XP_DARWIN */
const char* FFmpegRuntimeLinker::sLib = nullptr;
static unsigned (*avcodec_version)() = nullptr;

Expand All @@ -60,12 +70,36 @@ FFmpegRuntimeLinker::Link()

MOZ_ASSERT(NS_IsMainThread());

#ifdef XP_DARWIN // Explanation below.
char execPath[PATH_MAX];
execPath[0] = '\0';
uint32_t pathlen = PATH_MAX;
_NSGetExecutablePath(execPath, &pathlen);
char *execDir = dirname(execPath);
#endif /* XP_DARWIN */

for (size_t i = 0; i < ArrayLength(sLibs); i++) {
const char* lib = sLibs[i];
#ifdef XP_DARWIN
/* Loading FFMPEG on Mac OS X (macOS is a typo) fails because mozilla
searches for sybols defined in libavutil with a handle to libavcodec.
This is due to the fact that NSPR uses NSAddressOfSymbol & cie who limits
its researches only to libavcodec and not its dependencies. We don't have
this issue with dlsym(). */
if (!(sLinkedLib = dlopen(lib, RTLD_NOW | RTLD_LOCAL))) {
/* Bonus time: if we don't find libavcodec in standard locations, we look
if our venerable FFMPEG's libraries are in the same folder as XUL. */
char *libFullPath = NULL;
if (asprintf(&libFullPath, "%s/%s", execDir, lib) > 0 && libFullPath)
sLinkedLib = dlopen(libFullPath, RTLD_NOW | RTLD_LOCAL);
free(libFullPath);
}
#else
PRLibSpec lspec;
lspec.type = PR_LibSpec_Pathname;
lspec.value.pathname = lib;
sLinkedLib = PR_LoadLibraryWithFlags(lspec, PR_LD_NOW | PR_LD_LOCAL);
#endif /* XP_DARWIN */
if (sLinkedLib) {
if (Bind(lib)) {
sLib = lib;
Expand All @@ -92,7 +126,11 @@ FFmpegRuntimeLinker::Link()
/* static */ bool
FFmpegRuntimeLinker::Bind(const char* aLibName)
{
#ifdef XP_DARWIN
avcodec_version = (typeof(avcodec_version))dlsym(sLinkedLib,
#else
avcodec_version = (typeof(avcodec_version))PR_FindSymbol(sLinkedLib,
#endif /* XP_DARWIN */
"avcodec_version");
uint32_t fullVersion, major, minor, micro;
fullVersion = GetVersion(major, minor, micro);
Expand Down Expand Up @@ -139,6 +177,17 @@ FFmpegRuntimeLinker::Bind(const char* aLibName)
}

#define LIBAVCODEC_ALLVERSION
#ifdef XP_DARWIN
#define AV_FUNC(func, ver) \
if ((ver) & version) { \
if (!(func = (typeof(func))dlsym(sLinkedLib, #func))) { \
FFMPEG_LOG("Couldn't load function " #func " from %s.", aLibName); \
return false; \
} \
} else { \
func = (typeof(func))nullptr; \
}
#else
#define AV_FUNC(func, ver) \
if ((ver) & version) { \
if (!(func = (typeof(func))PR_FindSymbol(sLinkedLib, #func))) { \
Expand All @@ -148,6 +197,7 @@ FFmpegRuntimeLinker::Bind(const char* aLibName)
} else { \
func = (typeof(func))nullptr; \
}
#endif /* XP_DARWIN */
#include "FFmpegFunctionList.h"
#undef AV_FUNC
#undef LIBAVCODEC_ALLVERSION
Expand Down Expand Up @@ -181,7 +231,11 @@ FFmpegRuntimeLinker::CreateDecoderModule()
FFmpegRuntimeLinker::Unlink()
{
if (sLinkedLib) {
#ifdef XP_DARWIN
dlclose(sLinkedLib);
#else
PR_UnloadLibrary(sLinkedLib);
#endif /* XP_DARWIN */
sLinkedLib = nullptr;
sLib = nullptr;
sLinkStatus = LinkStatus_INIT;
Expand Down
4 changes: 4 additions & 0 deletions dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class FFmpegRuntimeLinker
static uint32_t GetVersion(uint32_t& aMajor, uint32_t& aMinor, uint32_t& aMicro);

private:
#ifdef XP_DARWIN
static void* sLinkedLib;
#else
static PRLibrary* sLinkedLib;
#endif /* XP_DARWIN */
static const char* sLib;

static bool Bind(const char* aLibName);
Expand Down

0 comments on commit 284830e

Please sign in to comment.