content/media/fmp4/ffmpeg/FFmpegRuntimeLinker.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     7 #include <dlfcn.h>
     9 #include "nsDebug.h"
    11 #include "FFmpegRuntimeLinker.h"
    13 // For FFMPEG_LOG
    14 #include "FFmpegDecoderModule.h"
    16 #define NUM_ELEMENTS(X) (sizeof(X) / sizeof((X)[0]))
    18 #define LIBAVCODEC 0
    19 #define LIBAVFORMAT 1
    20 #define LIBAVUTIL 2
    22 namespace mozilla
    23 {
    25 FFmpegRuntimeLinker::LinkStatus FFmpegRuntimeLinker::sLinkStatus =
    26   LinkStatus_INIT;
    28 static const char * const sLibNames[] = {
    29   "libavcodec.so.53", "libavformat.so.53", "libavutil.so.51",
    30 };
    32 void* FFmpegRuntimeLinker::sLinkedLibs[NUM_ELEMENTS(sLibNames)] = {
    33   nullptr, nullptr, nullptr
    34 };
    36 #define AV_FUNC(lib, func) typeof(func) func;
    37 #include "FFmpegFunctionList.h"
    38 #undef AV_FUNC
    40 /* static */ bool
    41 FFmpegRuntimeLinker::Link()
    42 {
    43   if (sLinkStatus) {
    44     return sLinkStatus == LinkStatus_SUCCEEDED;
    45   }
    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   }
    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
    63   sLinkStatus = LinkStatus_SUCCEEDED;
    64   return true;
    66 fail:
    67   Unlink();
    69   sLinkStatus = LinkStatus_FAILED;
    70   return false;
    71 }
    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 }
    85 } // namespace mozilla

mercurial