summaryrefslogtreecommitdiffstats
path: root/media/base/media_win.cc
blob: 616d3700631b81210e052b5fc34c39ae54e228c6 (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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/media.h"

#include <windows.h>
#if defined(_WIN32_WINNT_WIN8)
// The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h.
#undef FACILITY_VISUALCPP
#endif
#include <delayimp.h>

#include "base/file_path.h"
#include "base/logging.h"
#include "base/native_library.h"
#include "base/path_service.h"

#pragma comment(lib, "delayimp.lib")

namespace media {

// FFmpeg library name.
static const char* kFFmpegDLL = "ffmpegsumo.dll";

// Use a global to indicate whether the library has been initialized or not.  We
// rely on function level static initialization in InitializeMediaLibrary() to
// guarantee this is only set once in a thread safe manner.
static bool g_media_library_is_initialized = false;

static bool InitializeMediaLibraryInternal(const FilePath& base_path) {
  DCHECK(!g_media_library_is_initialized);

  // LoadLibraryEx(..., LOAD_WITH_ALTERED_SEARCH_PATH) cannot handle
  // relative path.
  if (!base_path.IsAbsolute())
    return false;

  // Use alternate DLL search path so we don't load dependencies from the
  // system path.  Refer to http://crbug.com/35857
  HMODULE lib = ::LoadLibraryEx(
      base_path.AppendASCII(kFFmpegDLL).value().c_str(), NULL,
      LOAD_WITH_ALTERED_SEARCH_PATH);

  // Check that we loaded the library successfully.
  g_media_library_is_initialized = (lib != NULL);
  return g_media_library_is_initialized;
}

bool InitializeMediaLibrary(const FilePath& base_path) {
  static const bool kMediaLibraryInitialized =
      InitializeMediaLibraryInternal(base_path);
  DCHECK_EQ(kMediaLibraryInitialized, g_media_library_is_initialized);
  return kMediaLibraryInitialized;
}

void InitializeMediaLibraryForTesting() {
  FilePath file_path;
  CHECK(PathService::Get(base::DIR_EXE, &file_path));
  CHECK(InitializeMediaLibrary(file_path));
}

bool IsMediaLibraryInitialized() {
  return g_media_library_is_initialized;
}

}  // namespace media