content/media/fmp4/ffmpeg/FFmpegRuntimeLinker.cpp

branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
equal deleted inserted replaced
-1:000000000000 0:9b3ab0ea7f99
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 <dlfcn.h>
8
9 #include "nsDebug.h"
10
11 #include "FFmpegRuntimeLinker.h"
12
13 // For FFMPEG_LOG
14 #include "FFmpegDecoderModule.h"
15
16 #define NUM_ELEMENTS(X) (sizeof(X) / sizeof((X)[0]))
17
18 #define LIBAVCODEC 0
19 #define LIBAVFORMAT 1
20 #define LIBAVUTIL 2
21
22 namespace mozilla
23 {
24
25 FFmpegRuntimeLinker::LinkStatus FFmpegRuntimeLinker::sLinkStatus =
26 LinkStatus_INIT;
27
28 static const char * const sLibNames[] = {
29 "libavcodec.so.53", "libavformat.so.53", "libavutil.so.51",
30 };
31
32 void* FFmpegRuntimeLinker::sLinkedLibs[NUM_ELEMENTS(sLibNames)] = {
33 nullptr, nullptr, nullptr
34 };
35
36 #define AV_FUNC(lib, func) typeof(func) func;
37 #include "FFmpegFunctionList.h"
38 #undef AV_FUNC
39
40 /* static */ bool
41 FFmpegRuntimeLinker::Link()
42 {
43 if (sLinkStatus) {
44 return sLinkStatus == LinkStatus_SUCCEEDED;
45 }
46
47 for (uint32_t i = 0; i < NUM_ELEMENTS(sLinkedLibs); i++) {
48 if (!(sLinkedLibs[i] = dlopen(sLibNames[i], RTLD_NOW | RTLD_LOCAL))) {
49 NS_WARNING("Couldn't link ffmpeg libraries.");
50 goto fail;
51 }
52 }
53
54 #define AV_FUNC(lib, func) \
55 func = (typeof(func))dlsym(sLinkedLibs[lib], #func); \
56 if (!func) { \
57 NS_WARNING("Couldn't load FFmpeg function " #func "."); \
58 goto fail; \
59 }
60 #include "FFmpegFunctionList.h"
61 #undef AV_FUNC
62
63 sLinkStatus = LinkStatus_SUCCEEDED;
64 return true;
65
66 fail:
67 Unlink();
68
69 sLinkStatus = LinkStatus_FAILED;
70 return false;
71 }
72
73 /* static */ void
74 FFmpegRuntimeLinker::Unlink()
75 {
76 FFMPEG_LOG("Unlinking ffmpeg libraries.");
77 for (uint32_t i = 0; i < NUM_ELEMENTS(sLinkedLibs); i++) {
78 if (sLinkedLibs[i]) {
79 dlclose(sLinkedLibs[i]);
80 sLinkedLibs[i] = nullptr;
81 }
82 }
83 }
84
85 } // namespace mozilla

mercurial