content/media/fmp4/ffmpeg/FFmpegRuntimeLinker.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/fmp4/ffmpeg/FFmpegRuntimeLinker.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include <dlfcn.h>
    1.11 +
    1.12 +#include "nsDebug.h"
    1.13 +
    1.14 +#include "FFmpegRuntimeLinker.h"
    1.15 +
    1.16 +// For FFMPEG_LOG
    1.17 +#include "FFmpegDecoderModule.h"
    1.18 +
    1.19 +#define NUM_ELEMENTS(X) (sizeof(X) / sizeof((X)[0]))
    1.20 +
    1.21 +#define LIBAVCODEC 0
    1.22 +#define LIBAVFORMAT 1
    1.23 +#define LIBAVUTIL 2
    1.24 +
    1.25 +namespace mozilla
    1.26 +{
    1.27 +
    1.28 +FFmpegRuntimeLinker::LinkStatus FFmpegRuntimeLinker::sLinkStatus =
    1.29 +  LinkStatus_INIT;
    1.30 +
    1.31 +static const char * const sLibNames[] = {
    1.32 +  "libavcodec.so.53", "libavformat.so.53", "libavutil.so.51",
    1.33 +};
    1.34 +
    1.35 +void* FFmpegRuntimeLinker::sLinkedLibs[NUM_ELEMENTS(sLibNames)] = {
    1.36 +  nullptr, nullptr, nullptr
    1.37 +};
    1.38 +
    1.39 +#define AV_FUNC(lib, func) typeof(func) func;
    1.40 +#include "FFmpegFunctionList.h"
    1.41 +#undef AV_FUNC
    1.42 +
    1.43 +/* static */ bool
    1.44 +FFmpegRuntimeLinker::Link()
    1.45 +{
    1.46 +  if (sLinkStatus) {
    1.47 +    return sLinkStatus == LinkStatus_SUCCEEDED;
    1.48 +  }
    1.49 +
    1.50 +  for (uint32_t i = 0; i < NUM_ELEMENTS(sLinkedLibs); i++) {
    1.51 +    if (!(sLinkedLibs[i] = dlopen(sLibNames[i], RTLD_NOW | RTLD_LOCAL))) {
    1.52 +      NS_WARNING("Couldn't link ffmpeg libraries.");
    1.53 +      goto fail;
    1.54 +    }
    1.55 +  }
    1.56 +
    1.57 +#define AV_FUNC(lib, func)                                                     \
    1.58 +  func = (typeof(func))dlsym(sLinkedLibs[lib], #func);                         \
    1.59 +  if (!func) {                                                                 \
    1.60 +    NS_WARNING("Couldn't load FFmpeg function " #func ".");                    \
    1.61 +    goto fail;                                                                 \
    1.62 +  }
    1.63 +#include "FFmpegFunctionList.h"
    1.64 +#undef AV_FUNC
    1.65 +
    1.66 +  sLinkStatus = LinkStatus_SUCCEEDED;
    1.67 +  return true;
    1.68 +
    1.69 +fail:
    1.70 +  Unlink();
    1.71 +
    1.72 +  sLinkStatus = LinkStatus_FAILED;
    1.73 +  return false;
    1.74 +}
    1.75 +
    1.76 +/* static */ void
    1.77 +FFmpegRuntimeLinker::Unlink()
    1.78 +{
    1.79 +  FFMPEG_LOG("Unlinking ffmpeg libraries.");
    1.80 +  for (uint32_t i = 0; i < NUM_ELEMENTS(sLinkedLibs); i++) {
    1.81 +    if (sLinkedLibs[i]) {
    1.82 +      dlclose(sLinkedLibs[i]);
    1.83 +      sLinkedLibs[i] = nullptr;
    1.84 +    }
    1.85 +  }
    1.86 +}
    1.87 +
    1.88 +} // namespace mozilla

mercurial