content/media/fmp4/ffmpeg/FFmpegRuntimeLinker.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <dlfcn.h>
michael@0 8
michael@0 9 #include "nsDebug.h"
michael@0 10
michael@0 11 #include "FFmpegRuntimeLinker.h"
michael@0 12
michael@0 13 // For FFMPEG_LOG
michael@0 14 #include "FFmpegDecoderModule.h"
michael@0 15
michael@0 16 #define NUM_ELEMENTS(X) (sizeof(X) / sizeof((X)[0]))
michael@0 17
michael@0 18 #define LIBAVCODEC 0
michael@0 19 #define LIBAVFORMAT 1
michael@0 20 #define LIBAVUTIL 2
michael@0 21
michael@0 22 namespace mozilla
michael@0 23 {
michael@0 24
michael@0 25 FFmpegRuntimeLinker::LinkStatus FFmpegRuntimeLinker::sLinkStatus =
michael@0 26 LinkStatus_INIT;
michael@0 27
michael@0 28 static const char * const sLibNames[] = {
michael@0 29 "libavcodec.so.53", "libavformat.so.53", "libavutil.so.51",
michael@0 30 };
michael@0 31
michael@0 32 void* FFmpegRuntimeLinker::sLinkedLibs[NUM_ELEMENTS(sLibNames)] = {
michael@0 33 nullptr, nullptr, nullptr
michael@0 34 };
michael@0 35
michael@0 36 #define AV_FUNC(lib, func) typeof(func) func;
michael@0 37 #include "FFmpegFunctionList.h"
michael@0 38 #undef AV_FUNC
michael@0 39
michael@0 40 /* static */ bool
michael@0 41 FFmpegRuntimeLinker::Link()
michael@0 42 {
michael@0 43 if (sLinkStatus) {
michael@0 44 return sLinkStatus == LinkStatus_SUCCEEDED;
michael@0 45 }
michael@0 46
michael@0 47 for (uint32_t i = 0; i < NUM_ELEMENTS(sLinkedLibs); i++) {
michael@0 48 if (!(sLinkedLibs[i] = dlopen(sLibNames[i], RTLD_NOW | RTLD_LOCAL))) {
michael@0 49 NS_WARNING("Couldn't link ffmpeg libraries.");
michael@0 50 goto fail;
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 #define AV_FUNC(lib, func) \
michael@0 55 func = (typeof(func))dlsym(sLinkedLibs[lib], #func); \
michael@0 56 if (!func) { \
michael@0 57 NS_WARNING("Couldn't load FFmpeg function " #func "."); \
michael@0 58 goto fail; \
michael@0 59 }
michael@0 60 #include "FFmpegFunctionList.h"
michael@0 61 #undef AV_FUNC
michael@0 62
michael@0 63 sLinkStatus = LinkStatus_SUCCEEDED;
michael@0 64 return true;
michael@0 65
michael@0 66 fail:
michael@0 67 Unlink();
michael@0 68
michael@0 69 sLinkStatus = LinkStatus_FAILED;
michael@0 70 return false;
michael@0 71 }
michael@0 72
michael@0 73 /* static */ void
michael@0 74 FFmpegRuntimeLinker::Unlink()
michael@0 75 {
michael@0 76 FFMPEG_LOG("Unlinking ffmpeg libraries.");
michael@0 77 for (uint32_t i = 0; i < NUM_ELEMENTS(sLinkedLibs); i++) {
michael@0 78 if (sLinkedLibs[i]) {
michael@0 79 dlclose(sLinkedLibs[i]);
michael@0 80 sLinkedLibs[i] = nullptr;
michael@0 81 }
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 } // namespace mozilla

mercurial