tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

OmxCoreLibLinker.cpp (3101B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "OmxCoreLibLinker.h"
      8 
      9 #include "MainThreadUtils.h"
     10 #include "PlatformDecoderModule.h"
     11 #include "mozilla/Preferences.h"
     12 #include "prlink.h"
     13 
     14 #ifdef LOG
     15 #  undef LOG
     16 #endif
     17 
     18 #define LOG(arg, ...)                        \
     19  MOZ_LOG(sPDMLog, mozilla::LogLevel::Debug, \
     20          ("OmxCoreLibLinker::%s: " arg, __func__, ##__VA_ARGS__))
     21 
     22 namespace mozilla {
     23 
     24 OmxCoreLibLinker::LinkStatus OmxCoreLibLinker::sLinkStatus = LinkStatus_INIT;
     25 
     26 const char* OmxCoreLibLinker::sLibNames[] = {
     27    "libopenmaxil.so",         // Raspberry Pi
     28    "libomxr_core.so",         // Renesas R-Car, RZ/G
     29    "libomxil-bellagio.so.0",  // Bellagio: An OSS implementation of OpenMAX IL
     30 };
     31 
     32 PRLibrary* OmxCoreLibLinker::sLinkedLib = nullptr;
     33 const char* OmxCoreLibLinker::sLibName = nullptr;
     34 
     35 #define OMX_FUNC(func) void (*func)();
     36 #include "OmxFunctionList.h"
     37 #undef OMX_FUNC
     38 
     39 bool OmxCoreLibLinker::TryLinkingLibrary(const char* libName) {
     40  PRLibSpec lspec;
     41  lspec.type = PR_LibSpec_Pathname;
     42  lspec.value.pathname = libName;
     43  sLinkedLib = PR_LoadLibraryWithFlags(lspec, PR_LD_NOW | PR_LD_LOCAL);
     44  if (sLinkedLib) {
     45    if (Bind(libName)) {
     46      sLibName = libName;
     47      LOG("Succeeded to load %s", libName);
     48      return true;
     49    } else {
     50      LOG("Failed to link %s", libName);
     51    }
     52    Unlink();
     53  }
     54  return false;
     55 }
     56 
     57 /* static */
     58 bool OmxCoreLibLinker::Link() {
     59  LOG("");
     60 
     61  if (sLinkStatus) {
     62    return sLinkStatus == LinkStatus_SUCCEEDED;
     63  }
     64 
     65  MOZ_ASSERT(NS_IsMainThread());
     66 
     67  nsAutoCString libPath;
     68  nsresult rv = Preferences::GetCString("media.omx.core-lib-path", libPath);
     69  if (NS_SUCCEEDED(rv) && !libPath.IsEmpty()) {
     70    if (TryLinkingLibrary(libPath.Data())) {
     71      sLinkStatus = LinkStatus_SUCCEEDED;
     72      return true;
     73    }
     74  }
     75 
     76  // try known paths
     77  for (size_t i = 0; i < std::size(sLibNames); i++) {
     78    if (TryLinkingLibrary(sLibNames[i])) {
     79      sLinkStatus = LinkStatus_SUCCEEDED;
     80      return true;
     81    }
     82  }
     83  sLinkStatus = LinkStatus_FAILED;
     84  return false;
     85 }
     86 
     87 /* static */
     88 bool OmxCoreLibLinker::Bind(const char* aLibName) {
     89 #define OMX_FUNC(func)                                              \
     90  {                                                                 \
     91    if (!(func = (typeof(func))PR_FindSymbol(sLinkedLib, #func))) { \
     92      LOG("Couldn't load function " #func " from %s.", aLibName);   \
     93      return false;                                                 \
     94    }                                                               \
     95  }
     96 #include "OmxFunctionList.h"
     97 #undef OMX_FUNC
     98  return true;
     99 }
    100 
    101 /* static */
    102 void OmxCoreLibLinker::Unlink() {
    103  LOG("");
    104 
    105  if (sLinkedLib) {
    106    PR_UnloadLibrary(sLinkedLib);
    107    sLinkedLib = nullptr;
    108    sLibName = nullptr;
    109    sLinkStatus = LinkStatus_INIT;
    110  }
    111 }
    112 
    113 }  // namespace mozilla