diff options
author | dalecurtis@chromium.org <dalecurtis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-06 22:55:21 +0000 |
---|---|---|
committer | dalecurtis@chromium.org <dalecurtis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-06 22:55:21 +0000 |
commit | 1dfde398066ef03904c0059ce0191b1b9a4aaeac (patch) | |
tree | f7ab8e2e84ee4cfa8e1286b240aa7c0b63904d0f | |
parent | 76ff1e3601252b13d0ad4b6521fbe64259fcf7fc (diff) | |
download | chromium_src-1dfde398066ef03904c0059ce0191b1b9a4aaeac.zip chromium_src-1dfde398066ef03904c0059ce0191b1b9a4aaeac.tar.gz chromium_src-1dfde398066ef03904c0059ce0191b1b9a4aaeac.tar.bz2 |
Remove function level static initializers media library initialization.
Moves media library initialization into a lazy initializer and dedups a
bunch of code. This will also be the eventual home for global media
initialization routines (YUV and VectorMath).
BUG=224662
TEST=compiles, media library works.
Review URL: https://chromiumcodereview.appspot.com/14891002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@198559 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | content/public/test/content_test_suite_base.cc | 2 | ||||
-rw-r--r-- | media/base/media.cc | 74 | ||||
-rw-r--r-- | media/base/media.h | 2 | ||||
-rw-r--r-- | media/base/media_android.cc | 25 | ||||
-rw-r--r-- | media/base/media_posix.cc | 33 | ||||
-rw-r--r-- | media/base/media_stub.cc | 25 | ||||
-rw-r--r-- | media/base/media_win.cc | 42 | ||||
-rw-r--r-- | media/base/run_all_unittests.cc | 13 | ||||
-rw-r--r-- | media/media.gyp | 8 |
9 files changed, 119 insertions, 105 deletions
diff --git a/content/public/test/content_test_suite_base.cc b/content/public/test/content_test_suite_base.cc index 046b94f..459c37d 100644 --- a/content/public/test/content_test_suite_base.cc +++ b/content/public/test/content_test_suite_base.cc @@ -22,6 +22,7 @@ #include "base/android/jni_android.h" #include "content/browser/android/browser_jni_registrar.h" #include "content/common/android/common_jni_registrar.h" +#include "media/base/android/media_jni_registrar.h" #include "net/android/net_jni_registrar.h" #include "ui/android/ui_jni_registrar.h" #include "ui/gl/android/gl_jni_registrar.h" @@ -54,6 +55,7 @@ void ContentTestSuiteBase::Initialize() { JNIEnv* env = base::android::AttachCurrentThread(); content::android::RegisterCommonJni(env); content::android::RegisterBrowserJni(env); + media::RegisterJni(env); net::android::RegisterJni(env); ui::android::RegisterJni(env); ui::shell_dialogs::RegisterJni(env); diff --git a/media/base/media.cc b/media/base/media.cc new file mode 100644 index 0000000..f70d7ca --- /dev/null +++ b/media/base/media.cc @@ -0,0 +1,74 @@ +// Copyright 2013 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 "base/files/file_path.h" +#include "base/lazy_instance.h" +#include "base/path_service.h" +#include "base/synchronization/lock.h" +#include "build/build_config.h" + +namespace media { + +namespace internal { +// Platform specific initialization method. +extern bool InitializeMediaLibraryInternal(const base::FilePath& module_dir); +} // namespace internal + +// Media must only be initialized once, so use a LazyInstance to ensure this. +class MediaInitializer { + public: + bool Initialize(const base::FilePath& module_dir) { + base::AutoLock auto_lock(lock_); + if (!tried_initialize_) { + tried_initialize_ = true; + initialized_ = internal::InitializeMediaLibraryInternal(module_dir); + } + return initialized_; + } + + bool IsInitialized() { + base::AutoLock auto_lock(lock_); + return initialized_; + } + + private: + friend struct base::DefaultLazyInstanceTraits<MediaInitializer>; + + MediaInitializer() + : initialized_(false), + tried_initialize_(false) { + // TODO(dalecurtis): Add initialization of YUV and VectorMath libraries. + } + + ~MediaInitializer() { + NOTREACHED() << "MediaInitializer should be leaky!"; + } + + base::Lock lock_; + bool initialized_; + bool tried_initialize_; + + DISALLOW_COPY_AND_ASSIGN(MediaInitializer); +}; + +static base::LazyInstance<MediaInitializer>::Leaky g_media_library = + LAZY_INSTANCE_INITIALIZER; + +bool InitializeMediaLibrary(const base::FilePath& module_dir) { + return g_media_library.Get().Initialize(module_dir); +} + +void InitializeMediaLibraryForTesting() { + base::FilePath module_dir; + CHECK(PathService::Get(base::DIR_EXE, &module_dir)); + CHECK(g_media_library.Get().Initialize(module_dir)); +} + +bool IsMediaLibraryInitialized() { + return g_media_library.Get().IsInitialized(); +} + +} // namespace media diff --git a/media/base/media.h b/media/base/media.h index 5518961..0eeb092 100644 --- a/media/base/media.h +++ b/media/base/media.h @@ -18,7 +18,7 @@ namespace media { // Attempts to initialize the media library (loading DLLs, DSOs, etc.). // -// If |module_dir| is the emptry string, then the system default library paths +// If |module_dir| is the empty string, then the system default library paths // are searched for the dynamic libraries. If a |module_dir| is provided, then // only the specified |module_dir| will be searched for the dynamic libraries. // diff --git a/media/base/media_android.cc b/media/base/media_android.cc index 8008d48..090e73a 100644 --- a/media/base/media_android.cc +++ b/media/base/media_android.cc @@ -6,31 +6,16 @@ #include <cpu-features.h> -#include "base/android/jni_android.h" -#include "base/logging.h" -#include "media/base/android/media_jni_registrar.h" +#include "base/files/file_path.h" namespace media { +namespace internal { -// Caches the result of the check for NEON support. -static bool g_media_library_is_initialized = false; - -bool InitializeMediaLibrary(const base::FilePath& module_dir) { +bool InitializeMediaLibraryInternal(const base::FilePath& module_dir) { // No real initialization is necessary but we do need to check if // Neon is supported because the FFT library requires Neon. - g_media_library_is_initialized = (android_getCpuFeatures() & - ANDROID_CPU_ARM_FEATURE_NEON) != 0; - return g_media_library_is_initialized; -} - -void InitializeMediaLibraryForTesting() { - // Register JNI bindings for android. - JNIEnv* env = base::android::AttachCurrentThread(); - RegisterJni(env); -} - -bool IsMediaLibraryInitialized() { - return g_media_library_is_initialized; + return (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0; } +} // namespace internal } // namespace media diff --git a/media/base/media_posix.cc b/media/base/media_posix.cc index 01d9e5f..243495a 100644 --- a/media/base/media_posix.cc +++ b/media/base/media_posix.cc @@ -22,6 +22,7 @@ using third_party_ffmpeg::StubPathMap; #endif // !defined(USE_SYSTEM_FFMPEG) namespace media { +namespace internal { // Handy to prevent shooting ourselves in the foot with macro wizardry. #if !defined(LIBAVCODEC_VERSION_MAJOR) || \ @@ -47,18 +48,11 @@ static const base::FilePath::CharType kSumoLib[] = #error "Do not know how to construct DSO name for this OS." #endif -// 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 base::FilePath& module_dir) { - DCHECK(!g_media_library_is_initialized); - +bool InitializeMediaLibraryInternal(const base::FilePath& module_dir) { #if defined(USE_SYSTEM_FFMPEG) // No initialization is necessary when using system ffmpeg, // we just link directly with system ffmpeg libraries. - g_media_library_is_initialized = true; + return true; #else StubPathMap paths; @@ -74,26 +68,9 @@ static bool InitializeMediaLibraryInternal(const base::FilePath& module_dir) { paths[kModuleFfmpegsumo].push_back(module_dir.Append( FILE_PATH_LITERAL(DSO_NAME("avformat", AVFORMAT_VERSION))).value()); - g_media_library_is_initialized = InitializeStubs(paths); + return InitializeStubs(paths); #endif // !defined(USE_SYSTEM_FFMPEG) - return g_media_library_is_initialized; -} - -bool InitializeMediaLibrary(const base::FilePath& base_path) { - static const bool kMediaLibraryInitialized = - InitializeMediaLibraryInternal(base_path); - DCHECK_EQ(kMediaLibraryInitialized, g_media_library_is_initialized); - return kMediaLibraryInitialized; -} - -void InitializeMediaLibraryForTesting() { - base::FilePath file_path; - CHECK(PathService::Get(base::DIR_EXE, &file_path)); - CHECK(InitializeMediaLibrary(file_path)); -} - -bool IsMediaLibraryInitialized() { - return g_media_library_is_initialized; } +} // namespace internal } // namespace media diff --git a/media/base/media_stub.cc b/media/base/media_stub.cc index aadfbd1..9efb37e 100644 --- a/media/base/media_stub.cc +++ b/media/base/media_stub.cc @@ -4,31 +4,16 @@ #include "media/base/media.h" -#include "base/logging.h" - -#if defined(OS_ANDROID) -#include "base/android/jni_android.h" -#include "media/base/android/media_jni_registrar.h" -#endif +#include "base/files/file_path.h" // This file is intended for platforms that don't need to load any media -// libraries (e.g., Android and iOS). +// libraries (e.g., iOS). namespace media { +namespace internal { -bool InitializeMediaLibrary(const base::FilePath& module_dir) { - return true; -} - -void InitializeMediaLibraryForTesting() { -#if defined(OS_ANDROID) - // Register JNI bindings for android. - JNIEnv* env = base::android::AttachCurrentThread(); - RegisterJni(env); -#endif -} - -bool IsMediaLibraryInitialized() { +bool InitializeMediaLibraryInternal(const base::FilePath& module_dir) { return true; } +} // namespace internal } // namespace media diff --git a/media/base/media_win.cc b/media/base/media_win.cc index b6602f9..43bf6a7 100644 --- a/media/base/media_win.cc +++ b/media/base/media_win.cc @@ -12,56 +12,28 @@ #include <delayimp.h> #include "base/files/file_path.h" -#include "base/logging.h" -#include "base/native_library.h" -#include "base/path_service.h" #pragma comment(lib, "delayimp.lib") namespace media { +namespace internal { -// 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 base::FilePath& base_path) { - DCHECK(!g_media_library_is_initialized); - +bool InitializeMediaLibraryInternal(const base::FilePath& module_dir) { // LoadLibraryEx(..., LOAD_WITH_ALTERED_SEARCH_PATH) cannot handle // relative path. - if (!base_path.IsAbsolute()) + if (!module_dir.IsAbsolute()) return false; // Use alternate DLL search path so we don't load dependencies from the // system path. Refer to http://crbug.com/35857 + static const char kFFmpegDLL[] = "ffmpegsumo.dll"; HMODULE lib = ::LoadLibraryEx( - base_path.AppendASCII(kFFmpegDLL).value().c_str(), NULL, + module_dir.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 base::FilePath& base_path) { - static const bool kMediaLibraryInitialized = - InitializeMediaLibraryInternal(base_path); - DCHECK_EQ(kMediaLibraryInitialized, g_media_library_is_initialized); - return kMediaLibraryInitialized; -} - -void InitializeMediaLibraryForTesting() { - base::FilePath file_path; - CHECK(PathService::Get(base::DIR_EXE, &file_path)); - CHECK(InitializeMediaLibrary(file_path)); -} - -bool IsMediaLibraryInitialized() { - return g_media_library_is_initialized; + return lib != NULL; } +} // namespace internal } // namespace media diff --git a/media/base/run_all_unittests.cc b/media/base/run_all_unittests.cc index c413874..899172b 100644 --- a/media/base/run_all_unittests.cc +++ b/media/base/run_all_unittests.cc @@ -4,9 +4,15 @@ #include "base/command_line.h" #include "base/test/test_suite.h" +#include "build/build_config.h" #include "media/base/media.h" #include "media/base/media_switches.h" +#if defined(OS_ANDROID) +#include "base/android/jni_android.h" +#include "media/base/android/media_jni_registrar.h" +#endif + class TestSuiteNoAtExit : public base::TestSuite { public: TestSuiteNoAtExit(int argc, char** argv) : TestSuite(argc, argv) {} @@ -18,6 +24,13 @@ class TestSuiteNoAtExit : public base::TestSuite { void TestSuiteNoAtExit::Initialize() { // Run TestSuite::Initialize first so that logging is initialized. base::TestSuite::Initialize(); + +#if defined(OS_ANDROID) + // Register JNI bindings for android. + JNIEnv* env = base::android::AttachCurrentThread(); + media::RegisterJni(env); +#endif + // Run this here instead of main() to ensure an AtExitManager is already // present. media::InitializeMediaLibraryForTesting(); diff --git a/media/media.gyp b/media/media.gyp index f447379..5f38e3e 100644 --- a/media/media.gyp +++ b/media/media.gyp @@ -259,6 +259,7 @@ 'base/djb2.h', 'base/filter_collection.cc', 'base/filter_collection.h', + 'base/media.cc', 'base/media.h', 'base/media_log.cc', 'base/media_log.h', @@ -560,6 +561,7 @@ ['include', '^audio/fake_audio_output_stream\\.'], ['include', '^base/audio_bus\\.'], ['include', '^base/channel_layout\\.'], + ['include', '^base/media\\.cc$'], ['include', '^base/media_stub\\.cc$'], ['include', '^base/media_switches\\.'], ['include', '^base/vector_math\\.'], @@ -586,6 +588,10 @@ 'player_android', 'video_capture_android_jni_headers', ], + 'sources': [ + 'base/media.cc', + 'base/media.h', + ], 'conditions': [ ['android_webview_build == 0', { 'dependencies': [ @@ -1188,7 +1194,7 @@ 'base/simd/convert_yuv_to_rgb_sse.asm', 'base/simd/convert_yuv_to_rgb_x86.cc', 'base/simd/convert_yuva_to_argb_mmx.asm', - 'base/simd/convert_yuva_to_argb_mmx.inc', + 'base/simd/convert_yuva_to_argb_mmx.inc', 'base/simd/empty_register_state_mmx.asm', 'base/simd/filter_yuv.h', 'base/simd/filter_yuv_c.cc', |