summaryrefslogtreecommitdiffstats
path: root/dom/media/platforms/omx/OmxCoreLibLinker.cpp
blob: a0ee61ec4224beec6d8c02eb0590362d731cb066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "OmxCoreLibLinker.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Preferences.h"
#include "MainThreadUtils.h"
#include "prlink.h"
#include "PlatformDecoderModule.h"

#ifdef LOG
#  undef LOG
#endif

#define LOG(arg, ...)                        \
  MOZ_LOG(sPDMLog, mozilla::LogLevel::Debug, \
          ("OmxCoreLibLinker::%s: " arg, __func__, ##__VA_ARGS__))

namespace mozilla {

OmxCoreLibLinker::LinkStatus OmxCoreLibLinker::sLinkStatus = LinkStatus_INIT;

const char* OmxCoreLibLinker::sLibNames[] = {
    "libopenmaxil.so",         // Raspberry Pi
    "libomxr_core.so",         // Renesas R-Car, RZ/G
    "libomxil-bellagio.so.0",  // Bellagio: An OSS implementation of OpenMAX IL
};

PRLibrary* OmxCoreLibLinker::sLinkedLib = nullptr;
const char* OmxCoreLibLinker::sLibName = nullptr;

#define OMX_FUNC(func) void (*func)();
#include "OmxFunctionList.h"
#undef OMX_FUNC

bool OmxCoreLibLinker::TryLinkingLibrary(const char* libName) {
  PRLibSpec lspec;
  lspec.type = PR_LibSpec_Pathname;
  lspec.value.pathname = libName;
  sLinkedLib = PR_LoadLibraryWithFlags(lspec, PR_LD_NOW | PR_LD_LOCAL);
  if (sLinkedLib) {
    if (Bind(libName)) {
      sLibName = libName;
      LOG("Succeeded to load %s", libName);
      return true;
    } else {
      LOG("Failed to link %s", libName);
    }
    Unlink();
  }
  return false;
}

/* static */
bool OmxCoreLibLinker::Link() {
  LOG("");

  if (sLinkStatus) {
    return sLinkStatus == LinkStatus_SUCCEEDED;
  }

  MOZ_ASSERT(NS_IsMainThread());

  nsAutoCString libPath;
  nsresult rv = Preferences::GetCString("media.omx.core-lib-path", libPath);
  if (NS_SUCCEEDED(rv) && !libPath.IsEmpty()) {
    if (TryLinkingLibrary(libPath.Data())) {
      sLinkStatus = LinkStatus_SUCCEEDED;
      return true;
    }
  }

  // try known paths
  for (size_t i = 0; i < ArrayLength(sLibNames); i++) {
    if (TryLinkingLibrary(sLibNames[i])) {
      sLinkStatus = LinkStatus_SUCCEEDED;
      return true;
    }
  }
  sLinkStatus = LinkStatus_FAILED;
  return false;
}

/* static */
bool OmxCoreLibLinker::Bind(const char* aLibName) {
#define OMX_FUNC(func)                                              \
  {                                                                 \
    if (!(func = (typeof(func))PR_FindSymbol(sLinkedLib, #func))) { \
      LOG("Couldn't load function " #func " from %s.", aLibName);   \
      return false;                                                 \
    }                                                               \
  }
#include "OmxFunctionList.h"
#undef OMX_FUNC
  return true;
}

/* static */
void OmxCoreLibLinker::Unlink() {
  LOG("");

  if (sLinkedLib) {
    PR_UnloadLibrary(sLinkedLib);
    sLinkedLib = nullptr;
    sLibName = nullptr;
    sLinkStatus = LinkStatus_INIT;
  }
}

}  // namespace mozilla