1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/media.h"
7 #include "base/files/file_path.h"
8 #include "base/lazy_instance.h"
9 #include "base/path_service.h"
10 #include "base/synchronization/lock.h"
11 #include "build/build_config.h"
12 #include "media/base/sinc_resampler.h"
13 #include "media/base/vector_math.h"
14 #include "media/base/yuv_convert.h"
19 // Platform specific initialization method.
20 extern bool InitializeMediaLibraryInternal(const base::FilePath
& module_dir
);
21 } // namespace internal
23 // Media must only be initialized once, so use a LazyInstance to ensure this.
24 class MediaInitializer
{
26 bool Initialize(const base::FilePath
& module_dir
) {
27 base::AutoLock
auto_lock(lock_
);
28 if (!tried_initialize_
) {
29 tried_initialize_
= true;
30 initialized_
= internal::InitializeMediaLibraryInternal(module_dir
);
35 bool IsInitialized() {
36 base::AutoLock
auto_lock(lock_
);
41 friend struct base::DefaultLazyInstanceTraits
<MediaInitializer
>;
44 : initialized_(false),
45 tried_initialize_(false) {
46 // Perform initialization of libraries which require runtime CPU detection.
47 // TODO(dalecurtis): Add initialization of YUV, SincResampler.
48 vector_math::Initialize();
49 SincResampler::InitializeCPUSpecificFeatures();
50 InitializeCPUSpecificYUVConversions();
54 NOTREACHED() << "MediaInitializer should be leaky!";
59 bool tried_initialize_
;
61 DISALLOW_COPY_AND_ASSIGN(MediaInitializer
);
64 static base::LazyInstance
<MediaInitializer
>::Leaky g_media_library
=
65 LAZY_INSTANCE_INITIALIZER
;
67 bool InitializeMediaLibrary(const base::FilePath
& module_dir
) {
68 return g_media_library
.Get().Initialize(module_dir
);
71 void InitializeMediaLibraryForTesting() {
72 base::FilePath module_dir
;
73 CHECK(PathService::Get(base::DIR_EXE
, &module_dir
));
74 CHECK(g_media_library
.Get().Initialize(module_dir
));
77 bool IsMediaLibraryInitialized() {
78 return g_media_library
.Get().IsInitialized();
81 void InitializeCPUSpecificMediaFeatures() {
82 // Force initialization of the media initializer, but don't call Initialize().
83 g_media_library
.Get();